raf*_*llo 2 javascript inheritance class
为什么Child类没有echo()方法?
Parent = function(){
this.name = 'abc';
}
Parent.prototype.echo = function(){
alert(this.name);
}
Child = function(){
$.extend(this, Parent);
}
var x = new Child();
x.echo();
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能继承Javascript中的父类?
您需要设置的原型Child来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();
Run Code Online (Sandbox Code Playgroud)