2012-01-02 16 views
6

मैं जावास्क्रिप्ट दुनिया में एक नौसिखिया हूं, और जब मैं प्रोटोटाइप चेनिंग विरासत का प्रयास कर रहा था तो मैं इस अजीब समस्या के साथ आया।जावास्क्रिप्ट प्रोटोटाइप चेनिंग सुपर क्लास कन्स्ट्रक्टर और विधि कॉलिंग

मैं सबसे पहले, मैं जिस तरह से अन्य OO भाषाओं में सुपर (args) को फोन करके वे करते हैं जैसे, माता पिता के वर्गों के निर्माता के लिए एक पैरामीटर पारित करने के लिए चाहता था 3 वर्गों

//class parent 
function parent(param_1){ 
    this.param = param_1; 
    this.getObjWithParam = function(val){ 
     console.log("value in parent class "+val); 
     console.log("Constructor parameter : "+this.param); 
    }; 

}; 

//class child 
function child(param_1){ 
    this.constructor(param_1); 
    this.getObjWithParam = function(val){ 
     console.log("value in child class "+val); 
     val = Number(val)+1; 
     child.prototype.getObjWithParam.call(this, [val]); 
    }; 
}; 
child.prototype = new parent(); 

//class grandChild 
function grandChild(param_1){ 
    this.constructor(param_1); 
}; 
grandChild.prototype = new child(); 


var gc = new grandChild(666); 
gc.getObjWithParam(0); 

है। तो this.constructor(param_1); काफी उद्देश्य के लिए उपयुक्त है।

हालांकि, उत्पादन,

value in parent class 0 
Constructor parameter : 666 

जिससे पता चलता है के रूप में आता है कि वर्ग पोता प्रोटोटाइप श्रृंखला को छोड़ दिया गया है, और बजाय getObjWithParam बच्चे() वर्ग के getObjWithParam() कॉल की, का आह्वान किया है की() अभिभावक वर्ग।

क्या किसी को भी कोई विचार है कि यहां क्या गलत है?

एनबी: 2 और निष्कर्ष जिन्हें मैं जोड़ना चाहता हूं, और दूसरा एक महत्वपूर्ण है। -> अगर मैं द्वारा

console.log(gc.constructor) 

पोता वर्ग के निर्माता खोजने की कोशिश उत्पादन मैं मिल

function parent(param_1){ 
    this.param = param_1; 
    this.getObjWithParam = function(val){ 
     console.log("value in parent class "+val); 
     console.log("Constructor parameter : "+this.param); 
    }; 
} 

जो काफी नहीं बात मैं उम्मीद है। मैं बाल वर्ग को देखने की उम्मीद कर रहा था।

-> अगर मैं टिप्पणी बच्चे में //this.constructor(param_1);() और पोता() वर्ग के लिए, कोड वास्तव में काम करता है के रूप में उम्मीद की कोशिश करो।

कोई भी इस घटना को समझा सकता है।

इसके अलावा, अगर कोई कामकाज का सुझाव दे तो इसकी अत्यधिक सराहना की जाएगी।

धन्यवाद

उत्तर

4

निर्माता समारोह में एक this.SOME_METHOD की घोषणा करने के लिए यह नहीं जोड़ता है प्रकार का प्रोटोटाइप। प्रोटोटाइप काम करता है, अलग से घोषित किए जाने की जरूरत उदा .:

//class parent 
function parent(param_1){ 
    console.log("parent " + param_1); 
    this.param = param_1; 
} 

parent.prototype.getObjWithParam = function(val) { 
    console.log("value in parent class "+val); 
    console.log("Constructor parameter : "+this.param); 
}; 

//class child 
function child(param_1){ 
    console.log("child " + param_1); 
    this.constructor(param_1); 
} 
child.prototype = new parent(); 
child.prototype.getObjWithParam = function(val) { 
    console.log("value in child class "+val); 
    val = Number(val)+1; 
    parent.prototype.getObjWithParam.call(this, [val]);  
} 

//class grandChild 
function grandChild(param_1){ 
    console.log("grandChild " + param_1); 
    this.constructor(param_1); 
} 
grandChild.prototype = new child(); 


var gc = new grandChild(666); 
gc.getObjWithParam(0); 

मैं तुम्हें this article को पढ़ने के लिए, एक गहरी अंतर्दृष्टि कैसे प्रोटोटाइप जावास्क्रिप्ट में काम पाने के लिए सिफारिश करेंगे।

+0

आपका उदाहरण और लेख मदद की थी, हालांकि, मुझे लगता है कि यह मुझे वापस वर्ग में लाता है। मैं प्रोटोटाइप चेनिंग विरासत में सुपर क्लास विधियों को कॉल करने का प्रयास कर रहा था। मैंने 'इस का उपयोग कर सुपर क्लास कन्स्ट्रक्टर विधि को कॉल करने का प्रयास किया।कन्स्ट्रक्टर (param_1); ' और 'class.prototype.function.call (यह, [param]) द्वारा सुपर क्लास विधि को कॉल करने का प्रयास किया;' तो मुझे लगता है, मैं सुपर क्लास विधि को जिस तरह से कॉल करता हूं उसे कॉल करूंगा कर रहे हैं, और '//this.constructor (param_1) का उपयोग करना बंद करें; इसके बजाय, मेरे पास विरासत वर्ग में प्रारंभिक चर शुरू करने के लिए कुछ विधि होगी, जिसे मैं बाल वर्ग से कॉल कर सकता हूं। – Tirtha

3

आप चेनिंग (Fluent Interface) करना चाहते हैं तो jQuery में की तरह:

<div id="div"></div> 

<script type="text/javascript"> 
function $(id) { 
    if(this.$) return new $.init(id); 
} 

$.init = function(id) { 
    if(typeof id == 'string') { 
     this.id = document.getElementById(id); 
    } 
}; 

$.init.prototype = $.prototype = { 
    constructor: $, 
    css: function(value) { 
     for(i in value) { 
      this.id.style[i] = value[i]; 
     } 
     return this; 
    }, 
    mush : function() { 
     var text = this.id.innerHTML; 
     this.id.innerHTML = text.split('').join('--'); 
     return this; 
    }, 
    text : function(a) { 
     this.id.innerHTML = a; 
     return this; 
    } 
}; 

$('div').text('FOO').mush().css({ 
     'color' : 'red', 
     'textTransform' : 'uppercase' 
}); 
</script> 

देखें example

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