gpt4 book ai didi

Javascript 继承 : How to

转载 作者:行者123 更新时间:2023-11-28 00:23:00 25 4
gpt4 key购买 nike

我在 .popin-foto 元素中打开一个 popin。当我尝试在同一元素中打开子类 popin 时,它不起作用。

代码

这是 parent

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

并且在控制台中,没有出现错误。

你能帮帮我吗?

最佳答案

看起来您的子类没有正确设置,因为您正在将子类的原型(prototype)设置为父类(super class)的具体实例。

我不确定你到底想达到什么目的,但我敢打赌子类构造函数需要直接调用父类(super class)构造函数,就像这样:

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

关于Javascript 继承 : How to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7679089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com