Search code examples
javascriptinternet-explorercross-browsergettergetter-setter

Cross-browser Getter and Setter


This works in modern Chrome/Firefox/Opera but fails in IE8. Haven't tried it in IE9. How can I make this cross-browser compatible, including IE7+? (Fiddle here.)

var foo = { 
    get test(){ return 'Works'; } 
};

// foo.test should be 'Works'

I've seen some usage with __defineGetter__ but that threw an 'unrecognized method' error in IE8.


Solution

  • I don't believe you can.

    In IE8 and lower, property access is mere property access. There's no way to run function code without explicitly invoking the function.

    I think in IE8 you may be able to with DOM elements, but I don't believe it works for regular native objects.