मैं पहले से ही js विरासत (this one, this one, this one, आदि)जावास्क्रिप्ट विरासत में सही प्रोटोटाइप प्रभाव क्या है?
पर बहुत सी लेख this article मोज़िला से में पढ़ते हैं, "क्लासिक" विरासत के रूप में दिखाया गया है: (मैं उदाहरण uniformized)
// inherit Base
function Derived() { ... }
Derived.prototype = new Base(); <-------
Derived.prototype.constructor = Derived; <-------
हालांकि this article में मैं देख रहा हूँ:
// inherit Base
function Derived() { ... }
Derived.prototype = Object.create(Base.prototype); <-------
Derived.prototype.constructor = Derived;
इसके अलावा मैं भी इस देखा है:
Derived.prototype = Base.prototype;
और मैं भी प्रयोग किया और constructor
दिखावा के उपयोग नहीं पा सके:
Derived.prototype.constructor = Derived; <--- it still work if I skip this line
अगर मैं इस लाइन को छोड़, new Derived()
सही ढंग से Derived()
वैसे भी कहते हैं।
तो 1) सही क्या है:
Derived.prototype = new Base();
Derived.prototype = Object.create(Base.prototype);
Derived.prototype = Base.prototype;
- अन्य?
और 2) Derived.prototype.constructor = Derived;
वास्तव में आवश्यक है? क्यूं कर ?
यह जावास्क्रिप्ट के बारे में बात है, आप जो भी कर सकते हैं वह कर सकते हैं .. मुझे जॉन रेसिग की कक्षा.जेएस पसंद है जो आपके लिए प्रोटोटाइप बकवास लपेटने का तरीका है, ताकि आपको इसे अपने कोड में कभी नहीं देखना पड़े .http: //ejohn.org/blog/simple-javascript-inheritance/ – andygoestohollywood
'Object.create()' अपेक्षाकृत हालिया कमांड है जो पहले मौजूद नहीं था। Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create देखें (ब्राउज़र संगतता अवलोकन उस पृष्ठ के नीचे है)। यदि आप केवल उन ब्राउज़रों का उपयोग करते हैं जिनके पास है, तो इसका उपयोग करें, अन्यथा नहीं - सलाह के हास्यास्पद रूप से स्पष्ट टुकड़े के लिए यह कैसा है? :) –