| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
Object.defineProperty() .
const object1 = {};
Object.defineProperty(object1, "property1", {
value: 42,
writable: false,
});
object1.property1 = 77;
// Throws an error in strict mode
console.log(object1.property1);
// Expected output: 42
Object.defineProperty(obj, prop, descriptor)
obj, .
prop Symbol .
descriptor.
. , ( for...in Object.keys), . .
, , : . , , ( ) . , . - ; .
, . :
configurable true , .
false.
enumerable true , .
false.
:
:
, , , . , Object, , Object.prototype.__proto__ null.
// __proto__
Object.defineProperty(obj, "key", {
__proto__: null, //
value: "static", //
// ,
//
//
});
//
Object.defineProperty(obj, "key", {
enumerable: false,
configurable: false,
writable: false,
value: "static",
});
//
function withValue(value) {
var d =
withValue.d ||
(withValue.d = {
enumerable: false,
writable: false,
configurable: false,
value: null,
});
d.value = value;
return d;
}
// ... ...
Object.defineProperty(obj, "key", withValue("static"));
// freeze,
// value, get, set, enumerable, writable configurable
// Object
(Object.freeze || Object)(Object.prototype);
, Object.defineProperty() . , . false. value, get set undefined. , get/set/value/writable , .
var o = {}; //
// defineProperty()
//
Object.defineProperty(o, "a", {
value: 37,
writable: true,
enumerable: true,
configurable: true,
});
// 'a' o , 37
// defineProperty()
//
var bValue = 38;
Object.defineProperty(o, "b", {
get: function () {
return bValue;
},
set: function (newValue) {
bValue = newValue;
},
enumerable: true,
configurable: true,
});
o.b; // 38
// 'b' o , 38
// o.b bValue ,
// o.b
// :
Object.defineProperty(o, "conflict", {
value: 0x9f91102,
get: function () {
return 0xdeadbeef;
},
});
// TypeError: value
// , get
//
, Object.defineProperty() .
configurable false, . , . writable: true writable true false. TypeError. value writable ( ) .
, undefined . , o.k , Object.defineProperty(o, "k", { set: undefined }) , k , . , ( undefined). ( ). , ( value writable), get set .
writable writable false, . .
var o = {}; //
Object.defineProperty(o, "a", {
value: 37,
writable: false,
});
console.log(o.a); // 37
o.a = 25; // (
// , )
console.log(o.a); // 37. .
, , .
enumerable enumerable , for...in Object.keys() .
var o = {};
Object.defineProperty(o, "a", { value: 1, enumerable: true });
Object.defineProperty(o, "b", { value: 2, enumerable: false });
// enumerable false
Object.defineProperty(o, "c", { value: 3 });
o.d = 4; // , enumerable
// true
for (var i in o) {
console.log(i);
}
// 'a' 'd' ( )
Object.keys(o); // ['a', 'd']
o.propertyIsEnumerable("a"); // true
o.propertyIsEnumerable("b"); // false
o.propertyIsEnumerable("c"); // false
configurable configurable , ( writable).
var o = {};
Object.defineProperty(o, "a", {
get: function () {
return 1;
},
configurable: false,
});
Object.defineProperty(o, "a", { configurable: true }); // TypeError
Object.defineProperty(o, "a", { enumerable: true }); // TypeError
Object.defineProperty(o, "a", { set: function () {} }); // TypeError ( set )
Object.defineProperty(o, "a", {
get: function () {
return 1;
},
}); // TypeError ( , get , )
Object.defineProperty(o, "a", { value: 12 }); // TypeError
console.log(o.a); // 1
delete o.a; //
console.log(o.a); // 1
configurable o.a true, .
, . Object.defineProperty(), .
var o = {};
o.a = 1;
// :
Object.defineProperty(o, "a", {
value: 1,
writable: true,
configurable: true,
enumerable: true,
});
// ,
Object.defineProperty(o, "a", { value: 1 });
// :
Object.defineProperty(o, "a", {
value: 1,
writable: false,
configurable: false,
enumerable: false,
});
, . temperature archive .
function Archiver() {
var temperature = null;
var archive = [];
Object.defineProperty(this, "temperature", {
get: function () {
console.log("get!");
return temperature;
},
set: function (value) {
temperature = value;
archive.push({ val: temperature });
},
});
this.getArchive = function () {
return archive;
};
}
var arc = new Archiver();
arc.temperature; // 'get!'
arc.temperature = 11;
arc.temperature = 13;
arc.getArchive(); // [{ val: 11 }, { val: 13 }]
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-object.defineproperty |
Object.defineProperties()Object.prototype.propertyIsEnumerable()Object.getOwnPropertyDescriptor()getsetObject.create()Reflect.defineProperty()This page was last modified on 29 . 2026 . by MDN contributors.
ObjectObject.assign()Object.create()Object.defineProperties()Object.defineProperty()Object.entries()Object.freeze()Object.fromEntries()Object.getOwnPropertyDescriptor()Object.getOwnPropertyDescriptors()Object.getOwnPropertyNames()Object.getOwnPropertySymbols()Object.getPrototypeOf()groupBy()hasOwn()Object.is()Object.isExtensible()Object.isFrozen()Object.isSealed()Object.keys()Object.preventExtensions()Object.seal()Object.setPrototypeOf()Object.values()Object.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |