I've always though the ptototype objects are not mutable after
instances have been made. I.e. changes to prototypes will not update
existing instances.
I've always done this:
Object.extend(MyObject, {bSwitch: false});
then MyObjec.bSwitch can be set/got globally
On 21/06/06, Sam <[EMAIL PROTECTED]> wrote:
I need a single boolean which would be "globally" accessible to all
instances of a class. Seems like the boolean should be in the class
prototype, but I was troubled by the difficulty of setting the prototype
boolean to true. Maybe I'm missing something?
var MyObject = Class.create();
MyObject.prototype = {
bSwitch: false,
... other methods and properties
}
var oMyOb1 = new MyObject();
var oMyOb2 = new MyObject();
alert(oMyOb1.bSwitch); // reports false
alert(oMyOb2.bSwitch); // reports false
Now set the value of bSwitch to true so all the oMyObN's will see a true
value.
I can see the following methods...
// __proto__ works in Firefox/Safari but isn't available in IE...
oMyOb1.__proto__.bSwitch = true;
alert(oMyOb1.bSwitch); // reports true
alert(oMyOb2.bSwitch); // reports true
// This changes bSwitch for a single instance. Not in the prototype.
oMyOb1.bSwitch = true;
alert(oMyOb1.bSwitch); // reports true
alert(oMyOb2.bSwitch); // reports false, not what we want.
// Seems strange to me to reference the Class, but this works...
MyObject.prototype.bSwitch = true;
alert(oMyOb1.bSwitch); // reports true
alert(oMyOb2.bSwitch); // reports true
// It's a little more satisfying to create a method to hide this reference
to the class...
MyObject.prototype = {
bSwitch: false,
setSwitch: function() {MyObject.prototype.bSwitch = true;}
}
// Now this works, and only requires a reference to a method on an instance
oMyOb1.setSwitch();
alert(oMyOb1.bSwitch); // reports true
alert(oMyOb2.bSwitch); // reports true
// I thought all objects had prototypes. Why doesn't this work?
oMyOb1.prototype.bSwitch = true; // error: oMyOb1 is not a function
Is there another method that I've missed? Did prototype.js extend
__proto__ to work in IE?
Sam
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
--
Andrew Tetlaw
htp://tetlaw.id.au
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs