Quiz
- can you explain why methods are functions?
Sure. A method is just a function that belongs to a particular class of object.
- what value is assigned to is when the normal functions change it?
if they are called like methods, the object
if they are called as normal functions, the global object
- Could we revisit the idea of an "ordinary" function again? How come that would have the wrong value for "this"?
Because it gets assigned to the global object, which is (almost) never what we want.
- What has the value of "this" changed into in the example of "hat.map(ron.deposit);"?
The global object, so the code
this.balance += amt
ends up incrementing a non-existent global variable. -
can you explain more on how
depositAll (hat) { hat.map(function (amt) { this.deposit(amt) }); }
this function works? thanks!Well, of course, it doesn't work: Because the anonymous function is called as an ordinary function, so
this
is the global object, and sothis.deposit
doesn't exist.But an arrow function would work.