Basically why I can't do this:
var a = [];
var b = a.push;
b(1);
it give's error, I know how to avoid this, but why this is not working in js?
Basically why I can't do this:
var a = [];
var b = a.push;
b(1);
it give's error, I know how to avoid this, but why this is not working in js?
The this
value is determined by how the function is called. You can use Function#bind
to ensure the this
value is set.
var a = [];
var b = a.push.bind(a);
b(1);
console.log(a);