Hi All,
After searching for what feels like a lifetime, I am now seeking the opinions of anyone who has already tried this, or even better has got it working:
Javascript allows the functionality of defining proper Object accessors using the [B]Object.defineProperty[B]__defineGetter__defineSetter
The code I have right now…
[CODE]var myObj = {},
if (Object.defineProperty) {
try {
Object.defineProperty(myObj, ‘myProperty’, {
get : function(){},
set : function(val){}
});
} catch(e) {
//IE8
}
} else
if (myObj.__defineGetter__) {
if (getter) {myObj.__defineGetter__(‘myProperty’, function(){});}
if (setter) {myObj.__defineSetter__(‘myProperty’, function(val){});}
} else {
//IE6/7
}
Now this works great if I support Internet Explorer from version 9.
My other option is to use “jQuery style” accessors, where you define a function wrapper to determine if the setter or getter function needs calling by specifying an argument:
[CODE]function Property(obj, name, getter, setter)
{
obj[name] = function()
{
return (arguments.length ? setter : getter).apply(obj, arguments);
};
}
This would obviously work in all browsers, but doesn’t look as nice as the first when assigning or retrieving values.
Before anyone asks… I do require accessors for argument evaluation, so I cannot just do straight forward assignments, and the defineProperty method of doing it would be first prize.
Many thanks.