javascript Object.defineProperty 适用于所有浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10282075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object.defineProperty for all browsers?
提问by Shane
Asking about Object.defineProperty as demonstrated below:
询问 Object.defineProperty 如下所示:
function testComponent(){
var testProperty;
Object.defineProperty(this, "testProperty",
{
get : function()
{
return testProperty;
},
set : function(val)
{
testProperty = val;
}
});
}
Where it would be used like so:
像这样使用它的地方:
testObject = new testComponent();
testObject.testProperty = "testValue";
Based on what I've seen so far, it looks like there is no cross browser solution, as I've tried using es5-shimwith no luck, but I would like to confirm. I also found a reference to this postand my tests still fail in IE 7 & 8, can anyone shed any light on this?
根据我目前所见,似乎没有跨浏览器解决方案,因为我尝试使用es5-shim但没有运气,但我想确认一下。我还找到了对这篇文章的引用,但我的测试在 IE 7 和 8 中仍然失败,有人能对此有所了解吗?
I remember looking through a related question a few months ago somewhere on S/O and I think I saw someone had written a solution for this in an answer. Any general workarounds for getter / setters would also be appreciated. The idea is that I need some equivalent of a getter setter on an object without passing the parameter change through a method. I don't need IE6, but I would like to support browsers in the range of IE7+ ff 3.6+ , etc
我记得几个月前在 S/O 的某个地方浏览了一个相关的问题,我想我看到有人在答案中为此编写了解决方案。getter / setter 的任何一般解决方法也将不胜感激。这个想法是我需要一个对象上的 getter setter 等价物,而无需通过方法传递参数更改。我不需要 IE6,但我想支持 IE7+ ff 3.6+ 等范围内的浏览器
QUnit tests below: (jsFiddles)
下面的 QUnit 测试:(jsFiddles)
(these pass in all browsers on my machine except IE 7 & 8
(这些通过我机器上的所有浏览器,除了 IE 7 和 8
direct use of defineProperty, no shims:
http://jsfiddle.net/uSYFE/
直接使用defineProperty,无垫片:http:
//jsfiddle.net/uSYFE/
fiddle using the ES5 shim, I'm assuming all I need to do is include it?:
http://jsfiddle.net/hyperthalamus/ntwDy/
小提琴使用 ES5 垫片,我假设我需要做的就是包括它?:
http://jsfiddle.net/hyperthalamus/ntwDy/
fiddle using the IE recommended solution:
http://jsfiddle.net/hyperthalamus/xfvz3/
使用 IE 推荐的解决方案小提琴:http:
//jsfiddle.net/hyperthalamus/xfvz3/
采纳答案by Florian Margaine
According to ES5-shim:
根据ES5-shim:
/!\ Object.defineProperty
This method will silently fail to set "writable", "enumerable", and "configurable" properties.
Providing a getter or setter with "get" or "set" on a descriptor will silently fail on engines that lack "defineGetter" and "defineSetter", which include all versions of IE up to version 8 so far.
IE 8 provides a version of this method but it only works on DOM objects. Thus, the shim will not get installed and attempts to set "value" properties will fail silently on non-DOM objects.
/!\ Object.defineProperty
此方法将无法设置“可写”、“可枚举”和“可配置”属性。
在缺少“defineGetter”和“defineSetter”的引擎上,在描述符上提供带有“get”或“set”的 getter 或 setter 将默默地失败,其中包括迄今为止版本 8 的所有 IE 版本。
IE 8 提供了此方法的一个版本,但它仅适用于 DOM 对象。因此,不会安装 shim,并且尝试设置“值”属性将在非 DOM 对象上静默失败。
So you know your answer. It can be done on DOM elements, that's it (and on IE8 only).
所以你知道你的答案。它可以在 DOM 元素上完成,就是这样(仅在 IE8 上)。
I'd suggest you just use get/set methods if you want IE7 to work.
如果你想让 IE7 工作,我建议你只使用 get/set 方法。
回答by MandoMando
For older IEs you'd have to make sure your property is a dom object (even a fake tag) and use onPropertyChange to get notified. See this post by John Dyerfor more details.
对于较旧的 IE,您必须确保您的属性是一个 dom 对象(甚至是假标签)并使用 onPropertyChange 来获得通知。有关更多详细信息,请参阅John Dyer 的这篇文章。
回答by ryanve
I've had this same question myself. (See here.)It doesn't look like it's fully possible in IE8 or lower. Otherwise the ES5 Shimis your best bet.
我自己也有过同样的问题。(请参阅此处。)在 IE8 或更低版本中,这似乎不是完全可能的。否则ES5 Shim是您最好的选择。