Suppose I have a Javascript object (the curly braces indicate it is so):
{
a: function (something) {
return something*2;
},
b: function () {
var c = this.a(2); //Does not work. Why?
return c;
}
}
What is the workaround to this?
It not work because when you access the method
bit context isn’t the instance from object that you made, it will try to search in the binded context or inwindowobject.This way you are using the
xas context to access the methoda.Or you can use a
newoperator to create your object and the method as it prototype or direct method.When you do it, the result will be:
It will make you lose the
prototypefromx, when you make a new instance, but your code will work.Example: