2011-10-06 17 views
2

我打開元素.popin-foto popin。當我嘗試在同一元素中打開子類popin時,它不起作用。JavaScript的繼承:如何

代碼

這是父

function Popin(container, titulo, url_listagem) { 
    this.url_listagem = url_listagem; 
    this.titulo = titulo; 
    this.overlay = $(".popin-overlay"); 
    this.closeButton = $(".popin-close"); 
    this.container = container; 
} 

Popin.prototype.header = function() { 
    var dados = {titulo: this.titulo}; 
    var html = $.tmpl("header", dados); 
    this.container.append(html); 
}; 

Popin.prototype.body = function() { 
    var html = $.tmpl("body"); 
    this.container.append(html); 
}; 

Popin.prototype.footer = function() { 
    var html = $.tmpl("footer"); 
    this.container.append(html); 
}; 

Popin.prototype.close = function() { 
    var self = this; 

    this.container.hide(100,function(){ 
     self.overlay.fadeOut('fast'); 
    }); 

    $(".popin-header").remove(); 
    $(".popin-body").remove(); 
    $(".popin-footer").remove(); 
}; 

Popin.prototype.open = function(){ 
    var self = this; 

    this.header(); 
    this.body(); 
    this.footer(); 

    this.closeButton.click(function(){ 
     self.close(); 
    }); 

    this.overlay.fadeTo("fast", 0.8, function(){ 
     self.container.show(); 
    }); 
}; 

子類

function PopinFoto(){} 

PopinFoto.prototype = new Popin($(".popin-fotos"), "fotos", "fake_url"); 
PopinFoto.prototype.open = function(){ 
    Popin.prototype.open.call(this); 
    $(".enviar-foto").die().live('click', function(){ 
     //do something 
    }); 
}; 

所以,我這樣做:

var popin = new Popin($(".popin-foto"), "title", "link"); 
popin.open(); 
popin.close(); 

var popinFoto = new PopinFoto($(".popin-foto"), "title", "link"); 
popinFoto.open(); //this not works 
popin.close(); 

而在控制檯中,沒有錯誤發生。

你能幫我嗎?

回答

2

看起來您的子類沒有正確設置,因爲您將子類的原型設置爲超類的具體實例。

我不知道你想實現什麼,但我敢打賭,該子類的構造函數需要直接調用超類的構造函數,像這樣:

function PopinFoto(container, titulo, url_listagem){ 
    Popin.call(this, container, titulo, url_listagem); 
} 
+0

我試試這個,但沒有工作。當我這樣做時,沒有將PopinFoto設置爲Popin的子類,所以它沒有父類的原型函數,例如this.body。 –