为什么子类没有 echo() 方法?
Parent = function(){
this.name = 'abc';
}
Parent.prototype.echo = function(){
alert(this.name);
}
Child = function(){
$.extend(this, Parent);
}
var x = new Child();
x.echo();
在Javascript中继承父类应该怎么做?
最佳答案
需要将Child
的原型(prototype)设置为Parent
。
function Parent() {
this.name = 'abc';
}
Parent.prototype.echo = function () {
alert(this.name);
}
function Child() {
}
Child.prototype = new Parent()
var x = new Child();
x.echo();
关于javascript - 如何扩展javascript类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3743142/