JavaScript this
this
inside callback to forEach
add
function above will cause following TypeError as this
inside the anonymous callback function is undefined.
This is because the callback function passed to forEach creates its own scope.
It makes this
inside the callback (this
=== undefined) and outside the callback (this
has access to sum: this.sum) different.
How to fix (bind) this
to callback function inside forEach:
Fix 1: optional argument to forEach
Syntax
forEach(callbackFn, thisArg)
thisArg (Optional): A value to use as
this
when executing callbackFn.
Fix 2: closure
Lexical context has changed inside the callback function.
The callback function still has access to variables in their enclosing scope (add(){...}
) throuth closure.
add
has access tothis
- callback doesn't have direct access to
this
- but callback has access to variables inside
add
- copy
this
insideadd
so callback can accessthis
Fix 3: bind
Explicitly set the value of this
within the callback function.
Excerpt from mdn: this
The bind() method can set the value of a function's this regardless of how it's called.
Fix 4: arrow function
Excerpt from mdn: this
Arrow functions don't provide their own this binding (it retains the this value of the enclosing lexical context).