//1、继承第一种方式:对象冒充
function Super(username){
this.username = username;
this.hello = function(){
alert(this.username);
};
}
function Sub(username){
this.super = Super;
this.super(username);
delete this.super;
this.world = function(){
alert(this.username+" Sub");
};
}
var superNew = new Super("super");
var subNew = new Sub("sub");
superNew.hello();
subNew.hello();
subNew.world();
//2、继承第二种方式:call()方法方式
/*
call方法是Function类中的方法
call方法的第一个参数的值赋值给类(即方法)中出现的this
call方法的第二个参数开始依次赋值给类(即方法)所接受的参数
*/
function TestCall(str){
alert(this.name + " " + str);
}
var object = new Object();
object.name = "upxiaofeng";
TestCall.call(object," bye bye");
function Super(){
this.show=function(){
alert("super");
};
}
function Sub(){
Super.call(this);
}
var superCall = new Super();
superCall.show();
var sbuCall = new Sub();
sbuCall.show();
//3、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承
function Super(){
}
Super.prototype.hello = "hello";
Super.prototype.sayHello = function(){
alert(this.hello);
};
function Sub(){
}
Sub.prototype = new Super();
Sub.prototype.world = "world";
Sub.prototype.sayWorld = function(){
alert(this.world);
};
var client = new Sub();
client.sayHello();
client.sayWorld();