Epigoon

Home

Tuesday, July 11, 2006

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.

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

Archive: current  October 2005  November 2005  December 2005  January 2006  February 2006  May 2006  July 2006 
Previously on Epigoon: New minimeter beta  Google Maps does rest of Europe?  Minimeter update  Geo browsing  Geo status indicator  Google Talk Maps  Minimeter and Feedview on CVS  Firefox 2.0 will include Feedview  JabberMaps for Servers  Ajax out of the box, XULRunner