admin管理员组文章数量:1290959
I am currently writing a JavaScript API which is based on new features in ES5. It uses Object.defineProperty
quite extensively. I have wrapped this into two new functions, called Object.createGetSetProperty
and Object.createValueProperty
I am however experiencing problems running this in older browsers (such as the dreaded, IE8)
Consider the following code:
Object.createGetSetProperty = function (object, property, get, set, enumerable, configurable) {
if (!Object.defineProperty) throw new Error("Object.defineProperty is not supported on this platform");
Object.defineProperty(object, property, {
get: get,
set: set,
enumerable: enumerable || true,
configurable: configurable || false
});
};
Object.createValueProperty = function (object, property, value, enumerable, configurable, writable) {
if (!Object.defineProperty) {
object[property] = value;
} else {
Object.defineProperty(object, property, {
value: value,
enumerable: enumerable || true,
configurable: configurable || false,
writable: writable || false
});
}
};
As you can see, there is a graceful fallback under Object.createValueProperty, but I've no idea how to fallback gracefully with Object.createGetSetProperty.
Does anyone know of any solutions, shims, polyfills for this?
I am currently writing a JavaScript API which is based on new features in ES5. It uses Object.defineProperty
quite extensively. I have wrapped this into two new functions, called Object.createGetSetProperty
and Object.createValueProperty
I am however experiencing problems running this in older browsers (such as the dreaded, IE8)
Consider the following code:
Object.createGetSetProperty = function (object, property, get, set, enumerable, configurable) {
if (!Object.defineProperty) throw new Error("Object.defineProperty is not supported on this platform");
Object.defineProperty(object, property, {
get: get,
set: set,
enumerable: enumerable || true,
configurable: configurable || false
});
};
Object.createValueProperty = function (object, property, value, enumerable, configurable, writable) {
if (!Object.defineProperty) {
object[property] = value;
} else {
Object.defineProperty(object, property, {
value: value,
enumerable: enumerable || true,
configurable: configurable || false,
writable: writable || false
});
}
};
As you can see, there is a graceful fallback under Object.createValueProperty, but I've no idea how to fallback gracefully with Object.createGetSetProperty.
Does anyone know of any solutions, shims, polyfills for this?
Share Improve this question asked Jun 24, 2013 at 8:39 Matthew LaytonMatthew Layton 42.4k57 gold badges207 silver badges336 bronze badges 2- 2 github./kriskowal/es5-shim – kangax Commented Jun 24, 2013 at 12:49
- developer.mozilla/en/docs/Web/JavaScript/Reference/… , They describe a polyfill at the end of the page – Jakob Sternberg Commented Mar 13, 2017 at 16:07
1 Answer
Reset to default 7For clarity, you might want to stick with standard terminology and name your routines defineDataProperty
and defineAccessorProperty
.
Also, your enumerable: enumerable || true
is going to result in a value of true
even if the caller passes in false...
Anyway, to get down to the question at hand: you can't do this in IE8. It is said that defineProperty
works in IE8 but only on DOM objects. There are ugly hacks for IE7 and below involving using the onpropertychanged
event on DOM objects. All of this has been gone over in some detail in other questions, such as Cross-browser Getter and Setter, JavaScript getter support in IE8, and many others.
本文标签: javascriptObjectdefineProperty polyfillStack Overflow
版权声明:本文标题:javascript - Object.defineProperty polyfill - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741508234a2382448.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论