← Back to JavaScript Course | Chapter 7: OOP & Prototypes | Lesson 3 of 6

JS का Object.create

Object.create एक नया object बनाता है जो किसी दूसरे object को अपने parent के रूप में इस्तेमाल करता है, जैसे किसी पुराने club के नियमों को follow करने वाला एक नया club शुरू करना। आप खुद parent चुनते हैं।
Syntax
javascript
const newObject = Object.create(prototypeObject);
const newObject = Object.create(prototypeObject, descriptors);

एक Object बनाना

Object.create आपके दिए गए prototype के साथ एक नया object बनाता है। यह किसी constructor function इस्तेमाल करने से inheritance सेट करने का ज़्यादा सीधा तरीका है, क्योंकि आप शुरू में ही exact prototype object बताते हैं।

उदाहरण: Creating an Object

javascript
// Declare the constant `proto`, set to `{ greet() { return "hi"; } }`
// Declare the constant `proto`, set to `{ greet() { return "hi"; } }`
const proto = { greet() { return "hi"; } };
// Declare the constant `obj`, set to `Object.create(proto)`
// Declare the constant `obj`, set to `Object.create(proto)`
const obj = Object.create(proto);
// Print `obj.greet()` to the console
// Print `obj.greet()` to the console
console.log(obj.greet());

Prototype के रूप में null इस्तेमाल करना

Object.create(null) एक ऐसा object बनाता है जिसकी prototype chain में Object.prototype नहीं होता।

यह एक genuinely 'खाली' object बनाने के लिए उपयोगी है जिसमें बिल्कुल कोई inherited methods न हों — यह तब एक साधारण {} से ज़्यादा सुरक्षित है जब आप accidental prototype pollution या collisions से बचना चाहते हैं।

उदाहरण: Using null as Prototype

javascript
const obj = Object.create(null);
obj.name = "Sam";
console.log(obj.name);
console.log(obj.toString); // undefined, no inherited methods

Properties जोड़ना

दूसरा argument object बनते समय properties define कर सकता है। एक descriptor object इस्तेमाल करना आपको बनते समय किसी property को non-writable या non-enumerable भी मार्क करने देता है, सिर्फ उसकी initial value सेट करने के अलावा।

उदाहरण: Adding Properties

javascript
const obj = Object.create({}, {
  name: { value: "Sam", writable: true },
});
// Print `obj.name` to the console
// Print `obj.name` to the console
console.log(obj.name);

Property Descriptors

Property descriptors नियंत्रित करते हैं कि कोई property बदली जा सकती है, loops में दिखती है, या हटाई जा सकती है। Descriptors ऐसा fine-grained control देते हैं जो plain assignment नहीं देता, जैसे किसी property को read-only बनाना या for...in loops और Object.keys() के लिए अदृश्य बनाना।

उदाहरण: Property Descriptors

javascript
const obj = {};
Object.defineProperty(obj, "id", { value: 1, writable: false, enumerable: false });
obj.id = 99; // silently fails, read-only
console.log(obj.id);
console.log(Object.keys(obj)); // [], hidden from enumeration

Practice में Object.create

Object.create तब उपयोगी है जब आपको shared behavior या कोई custom prototype चाहिए। इसका इस्तेमाल आमतौर पर तब किया जाता है जब आपको एक ऐसा object चाहिए जो किसी खास prototype के instance जैसा व्यवहार करे, बिना किसी constructor function या class से गुज़रे।

उदाहरण: Object.create in Practice

javascript
// Declare the constant `carPrototype`, set to `{ drive() { console.log("Driving"); } }`
// Declare the constant `carPrototype`, set to `{ drive() { console.log("Driving"); } }`
const carPrototype = { drive() { console.log("Driving"); } };
// Declare the constant `myCar`, set to `Object.create(carPrototype)`
// Declare the constant `myCar`, set to `Object.create(carPrototype)`
const myCar = Object.create(carPrototype);
// Call `myCar.drive()`
// Call `myCar.drive()`
myCar.drive();
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. #}
आम गलतियां
  1. Object.create() को कोई argument न देना, जो TypeError throw करता है; एक object या null pass करें।
  2. यह उम्मीद करना कि Object.create(obj) properties copy करेगा, जबकि यह obj को prototype बनाता है और नया object खाली शुरू होता है।
  3. Object.create(null) इस्तेमाल करना और उस पर toString() कॉल करना, जो fail होता है क्योंकि कोई prototype नहीं है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.