HTML and CSS Reference
In-Depth Information
It is now possible to execute the function as follows:
> add2()
70
We can use this method to solve the problem we encountered with inner functions in this
example:
> obj = { nums: 10,
add: function(num2) {
console.log('Outer this ' + this)
helper = function(num2) {
console.log('Inner this ' + this)
this.num += num2;
return this.num;
}
return helper(num2);
}
}
This can be rewritten as follows:
> obj = { num: 10,
add: function(num2) {
console.log('Outer this ' + this)
helper = function(num2) {
console.log('Inner this ' + this)
this.num += num2;
return this.num;
} .bind(this);
return helper(num2);
}
}
Executing this now provides the correct result:
> obj.add(20)
Search WWH ::




Custom Search