← Back to JavaScript Course | Chapter 2: Functions & Objects | Lesson 10 of 10

JS Object Protection

By default, any code with a reference to an object can freely add, change, or delete its properties -- which is not always desirable, like when passing a configuration object that should not be modified by whoever receives it. Object.freeze() and Object.seal() let you lock down an object's mutability to varying degrees.
Syntax
javascript
Object.freeze(object);
Object.seal(object);
Object.preventExtensions(object);
Object.defineProperty(object, "property", { writable: false });

Object.freeze() से Object को Freeze करना

Object.freeze(obj) किसी object को पूरी तरह लॉक कर देता है -- उसके बाद कोई properties जोड़ी, हटाई नहीं जा सकतीं और न ही उनके values बदले जा सकते हैं।

यह आमतौर पर उन values के लिए इस्तेमाल होता है जिन्हें constants या स्थिर configuration दर्शाना है जिसे कभी अनजाने में बदला नहीं जाना चाहिए।

Note: अनजाने में बदलाव की कोशिशें पकड़ने के लिए configuration objects, constants, या किसी भी ऐसे data को freeze कीजिए जिसे आप विशेष रूप से बनने के बाद केवल-पढ़ने योग्य रखना चाहते हैं।
Warning: In non-strict-mode code, attempting to mutate a frozen object fails silently with no error at all -- the assignment simply has no effect, which can hide a bug if you are not aware of this.

उदाहरण: Freezing an Object with Object.freeze()

javascript
const config = Object.freeze({ apiUrl: "/api" });
config.apiUrl = "/changed"; // silently fails
console.log(config.apiUrl);

जाँचना कि Object Frozen है या नहीं

Object.isFrozen(obj) true लौटाता है अगर object को freeze किया गया है, जिससे code बदलाव की कोशिश करने का फ़ैसला करने से पहले यह स्थिति जाँच सकता है, या यह debug करने के लिए कि अपेक्षित बदलाव क्यों नहीं हुआ।

Note: जब कोई बदलाव चुपचाप विफल होता लगे, तो यह पुष्टि करने के लिए कि object सचमुच frozen है या नहीं, debugging या रक्षात्मक code में Object.isFrozen() का उपयोग कीजिए।
Warning: A plain object literal is not frozen by default -- Object.isFrozen() returning true always means Object.freeze() (or an equivalent) was explicitly applied to it at some point.

उदाहरण: Checking If an Object Is Frozen

javascript
const obj = Object.freeze({ a: 1 });
console.log(Object.isFrozen(obj));

Object.seal() से Object को Seal करना

Object.seal(obj) freeze से हल्का प्रतिबंध है -- यह नई properties जोड़ने या मौजूदा को हटाने से रोकता है, लेकिन पहले से मौजूद properties के values बदलने की अनुमति फिर भी देता है, जो तब उपयोगी है जब object की संरचना स्थिर रहनी चाहिए पर उसका data फिर भी अपडेट हो सके।

Note: seal() को freeze() की जगह विशेष रूप से तब इस्तेमाल कीजिए जब object की properties का समूह स्थिर रहना चाहिए, पर values को समय के साथ वैध रूप से बदलना पड़ता है।
Warning: Confusing seal() with freeze() is common -- remember seal() still allows value changes, only locking the object's shape (which properties exist), not its content.

उदाहरण: Sealing an Object with Object.seal()

javascript
const user = Object.seal({ name: "Sam" });
user.name = "Amit"; // allowed, value can change
// user.age = 30; // not allowed, cannot add new property
console.log(user);

freeze() और seal() की Shallow प्रकृति

Object.freeze() और Object.seal() दोनों केवल object की अपनी सीधी properties की रक्षा करते हैं -- अगर उनमें से कोई property nested object रखती है, तो वह nested object पूरी तरह असुरक्षित और खुलकर बदलने योग्य रहता है, जब तक उसे अलग से freeze या seal न किया जाए।

Note: किसी nested संरचना की पूरी तरह रक्षा के लिए, हर nested object को अलग-अलग recursively freeze कीजिए, या deep-freeze helper function लिखिए (या ऐसी छोटी library इस्तेमाल कीजिए जो उसे देती हो)।
Warning: Assuming Object.freeze() protects an entire nested data structure is a very common misconception -- always verify whether the specific level you care about is actually frozen.

उदाहरण: The Shallow Nature of freeze() and seal()

javascript
const obj = Object.freeze({ nested: { value: 1 } });
obj.nested.value = 99; // still works! nested object is not frozen
console.log(obj.nested.value);

freeze(), seal() और दोनों में से कोई नहीं के बीच चुनना

सचमुच स्थिर, निश्चित data (जैसे enum-शैली की configuration) के लिए Object.freeze() इस्तेमाल कीजिए; उस data के लिए Object.seal() जिसकी संरचना स्थिर रहनी चाहिए पर values वैध रूप से बदलते हैं; और जब दोनों में से कोई गारंटी वास्तव में ज़रूरी न हो, तो object को पूरी तरह अप्रतिबंधित छोड़ दीजिए।

Note: freeze/seal को विशेष रूप से किसी असली invariant को लागू करने के लिए अपनाइए जिसकी आपको परवाह है (जैसे साझा configuration में अनजाने बदलाव रोकना), हर object के लिए डिफ़ॉल्ट आदत के रूप में नहीं।
Warning: Sealing or freezing an object that legitimately needs to be extended or restructured later can create confusing bugs where expected mutations silently fail.

उदाहरण: Choosing Between freeze(), seal(), and Neither

javascript
const MAX_USERS = Object.freeze({ limit: 100 }); // truly constant
const settings = Object.seal({ theme: "dark" }); // shape fixed, values can change
const data = { count: 0 }; // fully mutable
settings.theme = "light";
console.log(MAX_USERS, settings, data);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.