This is a follow up to a question I just posted.
I'm wondering how you all handle member variables in javascript clases when using MyClass.prototype to define methods.
If you define all of the methods in the constructor function:
function MyClass(){
this.myMethod = function(){}
}
You can very nicely declare member variables and access them from inside your methods:
function MyClass(){
var myVar = "hello";
this.myMethod = function(){
alert(myVar);
}
}
When using the Object.prototype technique, you lose this nicety, and have to do it like this;
function MyClass(){}
MyClass.prototype.myVar = "hello";
MyClass.prototype.myMethod = function(){alert(this.hello)};
I'm not crazy about having to write "this" every time I access a member variable. I want to use the Object.prototype approach for memory and flexibility reasons, but it seems a lot clumsier syntax-wise. Is this how you folks generally work?
thanks,
-Morgan
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…