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

JS प्रोटोटाइप चेन

Prototype chain वह तरीका है जिससे objects उन चीज़ों को ढूंढते हैं जो उनके पास नहीं होतीं, अपने parent से पूछकर, फिर parent के parent से, जैसे जवाब न पता होने पर अपने parents से पूछना। यह तब खत्म होता है जब पूछने के लिए कुछ नहीं बचता।
Syntax
javascript
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?

javascript
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

javascript
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

javascript
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

javascript
// 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

javascript
const obj = {};
console.log(Object.getPrototypeOf(Object.getPrototypeOf(obj))); // null, chain ends
console.log(obj.toString()); // inherited from Object.prototype
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 पर for...in इस्तेमाल करना और यह देखकर हैरान होना कि यह inherited properties भी list करता है।
  2. यह उम्मीद करना कि hasOwnProperty और in same तरीके से व्यवहार करेंगे, जबकि in prototype chain भी check करता है।
  3. Object.prototype या built-in prototypes बदलना, जो program के हर object को affect करता है।
🔒

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.