In the following code:
function xyz(x) {
//something
this.x = x;
}
xyz.prototype = {
a: function () {
//do something
},
b: function () {
//pre
this.a();
//post
}
}
the call of this.a() gives the warning of method not supported. So I tried using xyz.prototype.a.call(this) instead. But it does not maintain the value of x. What do I do to call one method of a class from other?
Given your code, if you write:
then calling
should correctly get to the “a()” function on the prototype. However, if you do something like this:
then it will not work, because there’s no context object (that is, the
thisvalue inside “b()” won’t be set correctly to an instance of “xyz”). That often happens when a function is being used as an event handler:The event handler, when called, won’t have an “xyz” instance to work with. Instead of that, therefore, you could write:
which clearly ensures that there’s an “xyz” object.