I was reading an article. It is related to javascript. Here, is a code sample from the article. Please look at below.
function customobject(){
this.value = 2;
}
customobject.prototype.inc = function(){
this.value++;
}
function changer(func){
func();
}
var o = new customobject();
alert(o.value);
o.inc();
alert(o.value);
changer(o.inc);
alert(o.value);
My question is Why is the value for "o.value" unchanged when changer(o.inc) is called ?
thisand you will see what happens. the problem is that you callincwithout the this context. If you want to make it work you have to dochanger(o.inc.bind(o)). bind doc