MyObject.prototype.bSwitch = true; is the correct way to achieve what you are looking for. Why would you want to do this by using an instance of said class? It seems to me you'd _want_ to _not_ have to know what instances are floating around in your application, so the "class" function's prototype is the natural place to make this change. In other words, you have 10 widgets and a couple of flibbets, maybe a few woozles on your page, each of them possibly containing N instances of MyObject. Why would you want to have to know the name of one of those instances to set this switch across all of them... see my point?
On 6/21/06, Andrew Tetlaw <[EMAIL PROTECTED]> wrote:
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
_______________________________________________ Rails-spinoffs mailing list [email protected] http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
