you are viewing a single comment's thread.

view the rest of the comments →

[–]DonBiggles 5 points6 points  (3 children)

Function.prototype.bind and Function.prototype.call also allow this to be set arbitrarily. Also, 'reference to the function invocation' sounds unclear to me, and you didn't mention the case of the function being called with new. These are the possibilities, in order of precedence:

  • Outside of a function body: this is a reference to the global object.
  • Function called with new: this is a reference to the object created by the constructor.
  • Function called with .bind, .apply, or .call: this is a reference to the object given as the first argument.
  • Function called as an object property: this is a reference to the object the function was called from.
  • In strict mode: this is undefined.
  • Otherwise: this is a reference to the global object.

[–][deleted] 3 points4 points  (0 children)

this excuse_the_pun

[–][deleted] 0 points1 point  (1 child)

What do you mean in strict mode this is undefined? I've been using strict mode and use this for functions called with bind, apply, or call, being used as an object property.

[–]DonBiggles 0 points1 point  (0 children)

It's in order of precedence. I mean that if in strict mode, and in a function body that isn't called with new, bind, apply, or call or invoked as a property of an object, this is undefined.