var Obj = function(par) {
this.par = par;
};
Obj.prototype.addPar = function(num) {
return num + this.par;
};
Obj.prototype.add3more = function(fun, num) {
return fun(num) + 3;
};
Obj.prototype.callAdd3more = function(num) {
return this.add3more(this.addPar, num);
};
var obj = new Obj(2);
console.log(obj.callAdd3more(1));
I would expect the code above to print out 5, but it prints out NaN. Somehow, addPar seems to get called out of the scope of the object obj
, because when it gets called this.par
is undefined.
I cannot understand why or how to correct the code.
Many thanks for your help.