I want to do something like this:
function x(){
var o = {};
o.__defineGetter__('y', function(){
return new Date();
});
return o.y;
}
var z = x();
console.log(z);
//Wait 1 second
console.log(z); //Date should be one second past the last printing
Of course, this doesn’t work because o.y is evaluated when it is returned. I’m looking for a way to return a variable that acts as a getter. The following example gives me hope that something like this could be possible:
function x(context){
//Bind the getter to the passed in scope
context.__defineGetter__('y', function(){
return new Date();
});
}
x(this);
console.log(y);
//Wait 1 second
console.log(y); //Date is one second past last printing
Has anyone ever tried to do something like this?
Yes, I’m familiar with other ways to model similar behavior using different syntax. I just want this particular syntax to work for a special scenario.
Thanks,
Chris
1 Answer