I'm using prototype.js (

Whether or not a class variable is static to a class or private to an
instance of that class (object) is not consistent.  Depending on the
type of the variable and how it is initialized, results may vary.

Here's an example:

var PersonClass1 = Class.create
    jobs: 0,
    friends: [],
    favoriteThings: {},
    initialize: function()

var PersonClass2 = Class.create
    initialize: function()
    { = 0;
      this.friends = [];
      this.favoriteThings = {};

//Define a couple instances of PersonClass1
var Bob = new PersonClass1();;
Bob.friends.push('Sal', 'Cal');
Bob.favoriteThings['movie'] = 'Office Space';
alert(;//1 as expected
alert(Bob.friends.length);//2 as expected
alert(Object.keys(Bob.favoriteThings).length);//1 as expected

var Ted = new PersonClass1();;
Ted.friends.push('Ron', 'Jon');
Ted.favoriteThings['candy'] = 'Good n Plenty';
alert(;//1 as expected
alert(Ted.friends.length);//4 as UNEXPECTED
alert(Object.keys(Ted.favoriteThings).length);//2 as UNEXPECTED

//Define a couple instances of PersonClass2
var Amy = new PersonClass2();;
Amy.friends.push('Ken', 'Ben');
Amy.favoriteThings['sport'] = 'Football';
alert(;//1 as expected
alert(Amy.friends.length);//2 as expected
alert(Object.keys(Amy.favoriteThings).length);//1 as expected

var Ana = new PersonClass2();;
Ana.friends.push('Pat', 'Sue');
Ana.favoriteThings['wine'] = 'Cabernet';
alert(;//1 as expected
alert(Ana.friends.length);//2 as expected
alert(Object.keys(Ana.favoriteThings).length);//1 as expected

As you can see, the outcomes are different depending on two things:
  1) class variable type
  2) class variable initialization

I'm not sure if this is a bug or not.  I haven't seen any
documentation (please enlighten me) that warns of this or recommends
the "safe" way to initialize class variables by ONLY using the
initialize method.

I'm okay with using the initialize method only, but I just hope this
doesn't bite anybody else in the future like it did me.

You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at

Reply via email to