JavaScript Functions
JavaScript
Method Invocation Pattern
Function defined as an object property - an object method:
JavaScript binds this
at execution (late binding), so this
refers to the myObject
object.
Function Invocation Pattern
Function invocation uses the format functionName()
. In this case, this
within the function refers to the global object. This can be incredibly confusing. The problem is evident when using an inner function within a method:
ES6 arrow functions provide a much less hacky solution, since the arrow function this
is inherited from the containing scope. The above example, rewritten with an arrow function:
comments powered by Disqus