| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze | [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.freeze() : , , , . , . .
Object.freeze(obj)
obj.
, . , , TypeError ( , , ).
- . - ( ) ( , ). , , , , .
var obj = {
prop: function () {},
foo: "bar",
};
// ,
obj.foo = "baz";
obj.lumpy = "woof";
delete obj.prop;
// , .
// .
var o = Object.freeze(obj);
o === obj; // true
Object.isFrozen(obj); // === true;
//
obj.foo = "quux"; //
obj.quaxxor = "the friendly duck"; //
// ... TypeError
function fail() {
"use strict";
obj.foo = "sparky"; // TypeError
delete obj.quaxxor; // TypeError
obj.sparky = "arf"; // TypeError
}
fail();
// Object.defineProperty
Object.defineProperty(obj, "ohai", { value: 17 }); // TypeError
Object.defineProperty(obj, "foo", { value: "eit" }); // TypeError
, - ( ).
obj = {
internal: {},
};
Object.freeze(obj);
obj.internal.a = "A";
obj.internal.a; // 'A'
// obj , obj.
// .
function deepFreeze(obj) {
// obj
var propNames = Object.getOwnPropertyNames(obj);
//
propNames.forEach(function (name) {
var prop = obj[name];
// prop,
if (typeof prop == "object" && prop !== null) deepFreeze(prop);
});
// obj ( , )
return Object.freeze(obj);
}
obj2 = {
internal: {},
};
deepFreeze(obj2);
obj2.internal.a = "";
obj2.internal.a; // undefined
ES5 ( ), TypeError. ES2015 , .
> Object.freeze(1)
TypeError: 1 is not an object // ES5
> Object.freeze(1)
1 // ES2015
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-object.freeze |
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 |