2013-04-08 6 views
7

में वर्ग विरासत निम्न उदाहरण की जाँच करें।कॉलिंग आधार वर्ग समारोह जावास्क्रिप्ट

मुझे कक्षा MyBaseClass का उत्तराधिकारी प्राप्त करने में सक्षम होना चाहिए, लेकिन फिर भी नई init() विधि की शुरुआत में उसकी init() विधि को कॉल करने में सक्षम होना चाहिए।

कैसे मैं ऐसा क्यों है?

+3

'$ .extend' जो आपको लगता है वह नहीं करता है। –

+1

इस कोड के साथ एक चमकदार समस्या का प्रकार नीचे –

+3

पर 'var' का उपयोग है' वैसे भी'extend' क्या है? –

उत्तर

17

jQuery's extend विरासत का निर्माण नहीं करता है लेकिन "एक साथ पहली वस्तु में दो या अधिक वस्तुओं की सामग्री को मर्ज करें"।

MyBaseClass = function(a) { 
    this.a = a; 
}; 
MyBaseClass.prototype.init = function() { 
    console.log('I am initializing the base class'); 
}; 

MyChildClass = function(a) { 
    this.a = a; 
} 
MyChildClass.prototype = Object.create(MyBaseClass.prototype); // makes MyChildClass "inherit" of MyBaseClass 
MyChildClass.prototype.init = function() { 
    MyBaseClass.prototype.init.call(this); // calls super init function 
    console.log('I am initializing the child class'); 
}; 

var child= new MyChildClass(); 
child.init(); 

Output:

I am initializing the base class 
I am initializing the child class 
+0

क्या 'ऑब्जेक्ट.क्रेट' का उपयोग यहां किया जाना चाहिए? –

+0

जैसे ओपी "वर्ग" और विरासत की बात करता है, मुझे व्यक्तिगत रूप से लगता है कि प्रोटोटाइप अधिक उपयुक्त है। –

+2

शायद जनवरी का मतलब 'MyChildClass.prototype = Object.create (MyBaseClass.prototype) का उपयोग करना था; '। विरासत आईएमएचओ के लिए यह एक बेहतर दृष्टिकोण होगा। अभिभावक के एक नए उदाहरण को तत्काल * * में कमी हो सकती है। –

1

jsFiddle Demo

चीजों की युगल

उपयोग prototype based inheritance अपने विरासत "सुपर" विधि को प्राप्त करने और स्पष्ट रूप से कॉल करने के लिए। extend वास्तव में गुणों पर जोड़ता है, यह बहुत कुछ नहीं करता है। इसलिए आपको अपनी कक्षा के लिए एक फ़ंक्शन तैयार करना होगा, बेस क्लास से प्राप्त होना चाहिए, और उसके बाद उस वर्ग प्रोटोटाइप पर विस्तार का उपयोग करना होगा। अपनी ही संपत्ति

function MyChildClass(){}; 
MyChildClass.prototype = new MyBaseClass(); 
MyChildClass.prototype.base = new MyBaseClass(); 
$.extend(MyChildClass.prototype, { 
init: function() { 
    this.base.init(); 
    console.log('I am initializing the child class'); 
} 
}); 
1

एक अन्य में आधार वर्ग स्टोर करने के लिए है जो - जब तरीकों की विशिष्टता एक मुद्दा होने जा रहा है -

function MyChildClass(){}; 
MyChildClass.prototype = new MyBaseClass(); 
$.extend(MyChildClass.prototype, { 
init: function() { 
    MyBaseClass.prototype.init(); 
    console.log('I am initializing the child class'); 
} 
}); 

यहाँ एक और दृष्टिकोण है कि मैं भाग के लिए उपयोग करना चाहते है

MyBaseClass = function(a) { 
    this.a = a; 
}; 

MyBaseClass.prototype = { 
    init: function() { 
     console.log('I am initializing the base class'); 
    } 
}; 

MyChildClass = function() {}; 
MyChildClass.prototype = $.extend(new MyBaseClass(), { 
    init: function() { 
     this.super.init.call(this); 
     console.log('init child'); 
    }, 
    super: MyBaseClass.prototype, 
    constructor: MyChildClass 
}); 

var a = new MyChildClass(); 
a.init(); 

आउटपुट::

प्रोटोटाइप आधारित पैटर्न इस लक्ष्य को प्राप्त करने के लिए 10

यहां this.super बेस क्लास के संदर्भ में स्टोर करता है।

+0

ईमानदार प्रश्न: आप कन्स्ट्रक्टर निर्दिष्ट क्यों कर रहे हैं? –

+0

@dystroy - मैंने वास्तव में jquery स्रोत कोड में देखा था।मुझे लगता है कि ऐसा इसलिए होता है क्योंकि कभी-कभी कन्स्ट्रक्टर कभी-कभी गलत फ़ंक्शन को इंगित करता है (इस समय इसे करने के लिए सटीक परिदृश्य को याद नहीं किया जा सकता है)। –

+0

@dystroy 'exampleof' का उपयोग करने में सक्षम होने के लिए। 'MyChildClass' – dfsq

संबंधित मुद्दे