Russell Tyndall's Tech Blog

The Shizzle Fa Rizzle at Acceleration.net

Home Contact Syndicate this Site (RSS 2.0) Syndicate this Site (Atom) Login
  80 Posts :: 6 Stories :: 69 Comments :: 19 Trackbacks

News

Listed on BlogShares

Archives

Post Categories

Image Galleries

Favorite Web Locations

Tech Tool Links


This is the only way that I have seen to create get / set accessors in Javascript. This seems very, very broken as this does not allow the object accessors to be created in a standard function constructor.
Is this truly the only way to do this?
I really hope not. There should be some way to get a referece to the chunk of code that is the accessor so that I can copy the accessor instead of copying the value in the accessor. This seems like something I should be able to do. For example, if I refer to an Number object (num for this) I receive the value stored in the object. I cannot duplicate this without someway to change the default behavior of both this and accessors. There has to be someway to get a reference to the thing returning a value. I just dont know where this is.
   ...
   var value = { 
      get ValueAccessor() { 
         return this.value+"*"; // note this.value is scoped to the object literal
      }, 
      set ValueAccessor( val ){
         this.value = val; // note this.value is scoped to the object literal
         return val;
      }
   }
   var x = newObject(value);
   ...
var constructTest = function(){
   var tmp = { 
      value:0,
      get Value(){
         return this.value+"*";
      },
      set Value(val){
         return (this.value=val);
      }
   }
   var self = new Object(tmp);
   self.name = "TestThisThought";
   return self;
}
var x = new constructTest(); // This will have the accessors
posted on Tuesday, November 02, 2004 2:01 PM