? And why is the
I hoped this will clear up any misunderstandings about the background of
to access and modify object properties as well to use customise its context in a smart and performant way.
Features, Attributes, and Techniques
Please refer to the following example:
is a property of the
object. And since it’s a function, it has to be invoked to determine whether the coffeeMaker needs a refill or not.
Comparison of call, apply, and bind
invoke a function. Their only difference is that
accepts arguments in a comma-separated fashion while
requires arguments to be passed as an array or an array-like object.
returns a function. Examples coming up.
Can you tell me when and why I should utilise them?
- “borrow” means “to use the properties or
- Making a unique version of this value
1. Using the functions of another object
How to Do It
is defined in the MDN docs:
call()allows for a function/method belonging to one object to be assigned and called for a different object.
That is to say, it is feasible to use one object’s method on another.
This may be seen in the
method belongs to the
object. However, we were able to call it on the
object! How magical is that?
A further instance of this is the common use of
to check if an object possesses a particular property. It’s recommended to (play it safe) use
instead of calling
directly on the object (which overlooks the fact that the
might not be a true instance of the
. For instance, if the
was created by calling
, then calling
will fail since the
does not have reference to the
. By using
, we are able to ‘borrow’ the
method from the Object.
Here, we’ll simplify matters by referring back to the aforementioned (call) instances but
Once again, the only difference between
is that with
you have to pass arguments as an array. Not doing so will fail with a
returns a function. For example:
and will be called on the
object. You can call
passing the arguments directly or pass the arguments in the bound function itself.
Two, make your own “this” value
Invoking a method on one object from inside another is feasible, as seen above. First, let’s back up a little.
Which term best describes this situation?
keyword is the ultimate cause of confusion for programmers because it’s used differently in object-oriented coding languages. Here’s my attempt at officially clearing things up:
Because of the, an object may examine and change its own characteristics.
keyword. In the
method is able to access its parent’s
Many people overestimate the importance of
in an object or in a function. Keep this in memory: the value of this is assigned when a method or function is invoked. And NOT where it is defined!
is being invoked, the value of
is assigned to the
. However, calling
without the dot will assign the value of this to the global / window object. See the example below:
is pointing to the window object which at that point doesn’t contain the
property. So again, to reiterate, the value of
is only assigned where a method or function is invoked but not where it is defined.
returns ‘Hello, undefined’. How can the undefined be resolved? Thanks to
(that we discussed above), we can assign the value of
to whatever we want!
There goes that bothersome problem! In other words, we tallied the worth of
object which in turn gives us access to the