Is there any way to make “private” variables (those defined in the constructor), available to prototype-defined methods?
TestClass = function(){
var priv
You need to change 3 things in your code:
var privateField = "hello" with this.privateField = "hello".privateField with this.privateField.privateField with this.privateField.The final code would be the following:
TestClass = function(){
this.privateField = "hello";
this.nonProtoHello = function(){alert(this.privateField)};
}
TestClass.prototype.prototypeHello = function(){alert(this.privateField)};
var t = new TestClass();
t.prototypeHello()