跨浏览器的Getter和Setter [英] Cross-browser Getter and Setter

查看:102
本文介绍了跨浏览器的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.

这篇关于跨浏览器的Getter和Setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆