JS का Object.create
In this page:
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
// 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
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
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
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
// 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();
Object.create()को कोई argument न देना, जोTypeErrorthrow करता है; एक object याnullpass करें।- यह उम्मीद करना कि
Object.create(obj)properties copy करेगा, जबकि यहobjको prototype बनाता है और नया object खाली शुरू होता है। Object.create(null)इस्तेमाल करना और उस परtoString()कॉल करना, जो fail होता है क्योंकि कोई prototype नहीं है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: