I'm currently studying javascript prototypes and I'm quite confused on how it really works. I have this code snippet and it works perfectly
function Message(msg){
this.msg =msg;
}
Message.prototype = {
constructor:Message,
display: function(){
return this.msg;
}
};
alert(new Message("007").display());
What this code is trying to do is to illustrate a simple encapsulation by using javascript.
Now the problem is, I want to add this text:this.msg instead of using the display method
Message.prototype = {
constructor:Message,
someValue:99,
text: this.msg,
display: function(){
return this.msg;
}
};
But i only get undefined when i call
alert(new Message("007").text);
But calling
alert(new Message("007").someValue);
Displays 99. What is the problem here?