プロトタイプで定義されたメソッドで使用できる「プライベート」変数 (コンストラクターで定義されたもの) を作成する方法はありますか?
TestClass = function(){
var privateField = "hello";
this.nonProtoHello = function(){alert(privateField)};
};
TestClass.prototype.prototypeHello = function(){alert(privateField)};
これは機能します:
t.nonProtoHello()
しかし、これはしません:
t.prototypeHello()
私はコンストラクター内でメソッドを定義することに慣れていますが、いくつかの理由でそれから離れています。