Javascript members constructor
I know this may seem obvious but I forget it everytime. So this is a reminder for myself and others. It takes a while to track this if you don't know it.
It's not that the first (red) is bad, it's just probably not what you want. In the red case there is one static memberA shared on all instanses of MyClass. In the green case memberA is just a normal member for each created instance.
function MyClass () {}
MyClass.prototype = {
memberA : new Array();
}
function MyClass () {
this.memberA = new Array();
}
MyClass.prototype = {
memberA : null
}
It's not that the first (red) is bad, it's just probably not what you want. In the red case there is one static memberA shared on all instanses of MyClass. In the green case memberA is just a normal member for each created instance.
0 Comments:
Post a Comment
<< Home