2011-10-06 6 views
2

요소 .popin-foto에 popin을 엽니 다. 동일한 요소에서 하위 클래스 popin을 열려고하면 작동하지 않습니다.자바 스크립트 상속 : How

이 부모에게있다

코드

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(); 
을3210

콘솔에서 오류가 발생하지 않았습니다.

도와 주시겠습니까?

답변

2

서브 클래스의 프로토 타입을 수퍼 클래스의 구체적인 인스턴스로 설정하면 서브 클래스가 제대로 설정되지 않은 것처럼 보입니다. 나는이 시도

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

:이 같은

난 당신이 달성하기 위해 노력하고 정확히 모르겠지만, 서브 클래스의 생성자가 직접 슈퍼 클래스의 생성자를 호출 할 필요가 있음을 내기 것 그러나 일하지 않았다. 이 작업을 수행 할 때 Popin의 하위 클래스처럼 PopinFoto가 설정되어 있지 않으므로 parent와 같은 프로토 타입 함수가 없습니다. 예를 들면. –