--- title: Object.defineProperties() slug: Web/JavaScript/Reference/Global_Objects/Object/defineProperties translation_of: Web/JavaScript/Reference/Global_Objects/Object/defineProperties ---
Object.defineProperties() 函式可定義新的或是修改已存在的物件屬性,並回傳修改後的物件。
Object.defineProperties(obj, props)
objpropsconfigurabletrue if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.false.enumerable若該屬性設為 true,則該屬性可被物件所列舉。false.valuewritable若該屬性為 true,則該屬性可透過{{jsxref("Operators/Assignment_Operators", "賦予運算子", "", 1)}}所改變false.getsetThe object that was passed to the function.
Object.defineProperties, in essence, defines all properties corresponding to the enumerable own properties of props on the object obj object.
var obj = {};
Object.defineProperties(obj, {
'property1': {
value: true,
writable: true
},
'property2': {
value: 'Hello',
writable: false
}
// etc. etc.
});
Assuming a pristine execution environment with all names and properties referring to their initial values, Object.defineProperties is almost completely equivalent (note the comment in isCallable) to the following reimplementation in JavaScript:
function defineProperties(obj, properties) {
function convertToDescriptor(desc) {
function hasProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function isCallable(v) {
// NB: modify as necessary if other values than functions are callable.
return typeof v === 'function';
}
if (typeof desc !== 'object' || desc === null)
throw new TypeError('bad desc');
var d = {};
if (hasProperty(desc, 'enumerable'))
d.enumerable = !!desc.enumerable;
if (hasProperty(desc, 'configurable'))
d.configurable = !!desc.configurable;
if (hasProperty(desc, 'value'))
d.value = desc.value;
if (hasProperty(desc, 'writable'))
d.writable = !!desc.writable;
if (hasProperty(desc, 'get')) {
var g = desc.get;
if (!isCallable(g) && typeof g !== 'undefined')
throw new TypeError('bad get');
d.get = g;
}
if (hasProperty(desc, 'set')) {
var s = desc.set;
if (!isCallable(s) && typeof s !== 'undefined')
throw new TypeError('bad set');
d.set = s;
}
if (('get' in d || 'set' in d) && ('value' in d || 'writable' in d))
throw new TypeError('identity-confused descriptor');
return d;
}
if (typeof obj !== 'object' || obj === null)
throw new TypeError('bad obj');
properties = Object(properties);
var keys = Object.keys(properties);
var descs = [];
for (var i = 0; i < keys.length; i++)
descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
for (var i = 0; i < descs.length; i++)
Object.defineProperty(obj, descs[i][0], descs[i][1]);
return obj;
}
| Specification | Status | 註記 |
|---|---|---|
| {{SpecName('ES5.1', '#sec-15.2.3.7', 'Object.defineProperties')}} | {{Spec2('ES5.1')}} | Initial definition. Implemented in JavaScript 1.8.5 |
| {{SpecName('ES6', '#sec-object.defineproperties', 'Object.defineProperties')}} | {{Spec2('ES6')}} | |
| {{SpecName('ESDraft', '#sec-object.defineproperties', 'Object.defineProperties')}} | {{Spec2('ESDraft')}} |
| Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | {{CompatGeckoDesktop("2")}} | {{CompatChrome("5")}} | {{CompatIE("9")}} | {{CompatOpera("11.60")}} | {{CompatSafari("5")}} |
| Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | {{CompatGeckoMobile("2")}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatOperaMobile("11.5")}} | {{CompatVersionUnknown}} |