跨浏览器的Getter和Setter

问题描述:

这适用于现代的Chrome / Firefox / Opera,但在IE8中失败。没有尝试过它在IE9。如何使这个跨浏览器兼容,包括IE7 +? (小提琴在这里。)

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'

看到一些使用 __ defineGetter __ ,但是在IE8中引发了一个无法识别的方法错误。

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

我不相信你可以。

I don't believe you can.

IE8 以下,属性访问只是属性访问。没有方法来运行函数代码,而没有显式调用该函数。

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

我认为在IE8你可以使用DOM元素,但我不相信它的工作原理常规本机对象。

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