Setup:
I'm using prototype.js (1.6.0.2)

Problem:
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()
    {
      this.jobs = 0;
      this.friends = [];
      this.favoriteThings = {};
    }
  }
);


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

var Ted = new PersonClass1();
Ted.jobs++;
Ted.friends.push('Ron', 'Jon');
Ted.favoriteThings['candy'] = 'Good n Plenty';
alert(Ted.jobs);//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.jobs++;
Amy.friends.push('Ken', 'Ben');
Amy.favoriteThings['sport'] = 'Football';
alert(Amy.jobs);//1 as expected
alert(Amy.friends.length);//2 as expected
alert(Object.keys(Amy.favoriteThings).length);//1 as expected

var Ana = new PersonClass2();
Ana.jobs++;
Ana.friends.push('Pat', 'Sue');
Ana.favoriteThings['wine'] = 'Cabernet';
alert(Ana.jobs);//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.


Thoughts?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to