I have a "class" within Javascript with a variable assigned in the constructor. I also use prototype to store variables/methods:
var MyClass = function()
{
this.age = 100;
};
MyClass.prototype.name = "John";
MyClass.prototype.getAge = function() { return this.age};
alert(MyClass.prototype.name); //Alerts John
alert(MyClass.age); //undefined as expected
alert(MyClass.prototype.getAge()); //undefined??
So from what I can tell, there is no way I can access the constructor variables that are stored within MyClass unless I create an object from the Class:
var theClass = new MyClass();
alert(theClass.age);
alert(theClass.getAge());
Both alerts will return the age correctly.
So to reiterate, can I access the constructor variables directly from the MyClass without needing to create a variable?
alert(MyClass.prototype.getAge()); //undefined??: It would try to accessMyClass.prototype.agewhich does not exist. Inside the constructorthisrefers to an empty object with inherits fromMyClass.prototype, soagewill never be assigned toMyClass.prototype.