我目前正在編寫一個基於ES5新功能的JavaScript API。它相當廣泛地使用Object.defineProperty
。我已經纏到這兩個新的功能,稱爲Object.createGetSetProperty
和Object.createValueProperty
Object.defineProperty polyfill
不過,我遇到了舊的瀏覽器運行此問題(如可怕的,IE8)
考慮下面的代碼:
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
});
}
};
正如你所看到的,在Object.createValueProperty下有一個優雅的回退,但我不知道如何使用Object.createGetSetProperty正常回退。
有沒有人知道任何解決方案,墊片,polyfills爲此?
https://github.com/kriskowal/es5-shim – kangax
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties,他們在最後描述了一個polyfill的頁面 –