JS प्रोटोटाइप चेन
In this page:
object.__proto__
Object.getPrototypeOf(object);
Constructor.prototype.methodName = function() { };
Prototype Chain क्या है?
JavaScript objects दूसरे objects से properties inherit कर सकते हैं। यह lookup एक prototype chain के ज़रिए जारी रहता है।
यही mechanism किसी built-in type पर हर method call के पीछे है — .map() जैसे array methods हर array instance पर store नहीं होते, वे सिर्फ एक बार Array.prototype पर रहते हैं।
उदाहरण: What Is the Prototype Chain?
const arr = [1, 2, 3];
console.log(arr.map === Array.prototype.map); // true, shared on the prototype
Prototypes जांचना
आप Object.getPrototypeOf से किसी object का prototype देख सकते हैं। यह inheritance के लिए इस्तेमाल होने वाला object return करता है।
यह runtime पर inheritance relationships confirm करने के लिए उपयोगी है, जैसे यह verify करना कि कोई object असल में किसी खास constructor के prototype से बनाया गया था।
उदाहरण: Checking Prototypes
const arr = [];
console.log(Object.getPrototypeOf(arr) === Array.prototype);
Inherited Properties
कोई property खुद object की हो सकती है या उसके prototype से आ सकती है। in operator दोनों जगह जांचता है।
इस lookup में अपनी properties को हमेशा प्राथमिकता मिलती है, इसीलिए आप object पर सीधे एक same-named property assign करके किसी inherited default को override कर सकते हैं।
उदाहरण: Inherited Properties
const obj = { greet() {} };
console.log("greet" in obj); // true, own property
console.log("toString" in obj); // true, inherited from prototype
Prototype Values को Override करना
एक object किसी prototype property जैसे ही नाम की अपनी property define कर सकता है। खुद की property को पहले इस्तेमाल किया जाता है।
यह किसी खास instance को व्यवहार customize करने देता है — जैसे कोई special-case object किसी आमतौर पर shared method को override कर दे — बाकी हर instance के लिए shared prototype को बदले बिना।
उदाहरण: Overriding Prototype Values
// Declare the constant `proto`, set to `{ greet() { return "from prototype"; } }`
// Declare the constant `proto`, set to `{ greet() { return "from prototype"; } }`
const proto = { greet() { return "from prototype"; } };
// Declare the constant `obj`, set to `Object.create(proto)`
// Declare the constant `obj`, set to `Object.create(proto)`
const obj = Object.create(proto);
// Assign `() => "from own property"` to `obj.greet`
// Assign `() => "from own property"` to `obj.greet`
obj.greet = () => "from own property";
// Print `obj.greet()` to the console
// Print `obj.greet()` to the console
console.log(obj.greet());
Prototype Chain Lookup
JavaScript पहले object को जांचता है, फिर उसके prototype को, और null तक पहुंचने तक जांचता रहता है।
हर prototype chain आखिरकार Object.prototype पर और फिर null पर खत्म होती है, इसीलिए plain objects डिफ़ॉल्ट रूप से toString() जैसे आम methods अभी भी inherit करते हैं।
उदाहरण: Prototype Chain Lookup
const obj = {};
console.log(Object.getPrototypeOf(Object.getPrototypeOf(obj))); // null, chain ends
console.log(obj.toString()); // inherited from Object.prototype
- किसी object पर
for...inइस्तेमाल करना और यह देखकर हैरान होना कि यह inherited properties भी list करता है। - यह उम्मीद करना कि
hasOwnPropertyऔरinsame तरीके से व्यवहार करेंगे, जबकिinprototype chain भी check करता है। Object.prototypeया built-in prototypes बदलना, जो program के हर object को affect करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: