My typical JS class structure looks like this:
MyClass = function(args)
{
this.myProp1 = undefined;
this.myProp2 = args[0];
//...more member data
this.foo = function()
{
return this.myProp1 + this.myProp2; //<- the problem.
}
//...more member functions
}
//if MyClass extends a superclass, add the following...
MyClass.prototype = Object.create(MySuperClass.prototype);
MyClass.prototype.constructor = MyClass;
...My ongoing annoyance with JS is that I seem to have to use this
continuously in member functions in order to access the properties of the very same object to which those functions belong. In several other languages e.g. C# & Java, this
may be safely omitted when member functions are working with member data in the same class / instance. (I realise that JS is structured fundamentally differently due to it being designed as a prototypal rather than a hierarchical inheritance language.)
To put the question another way: Is there any way to make the unspecified scope NOT point to window
, but rather to the current, local value of this
?
P.S. I am guessing this is a language limitation, but thought I'd check again anyway.