javascript继承

javascript继承
function Parent(name){
this.name = name;
}
Parent.prototype.say = function(){
alert(this.name);
}
var parent = new Parent("wwwwww");
parent.say();
function Child(name,password){
Parent.call(this,name);
this.password = password;
}
Child.prototype = new Parent();
Child.prototype.sayword = function(){
alert(this.password);
}
var child = new Child("111111","222222222");
child.sayword();

////用call实现继承
function Parent(name){
this.name = name;
this.say = function(){
alert(this.name );
}
}
function Child(name,password){
Parent.call(this,name);
this.password = password;
}
var child = new Child("123123","sssss");
child.say();
*/
//用APPLY实现继承
/*
function Parent(name){
this.name= name;
this.say= function(){
alert(this.name);
}
}
function Child(name,password){
Parent.apply(this,new Array(name));
this.password = password;
this.sayworld = function(){
alert(this.password + this.name);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.say();
child.say();
child.sayworld();




你可能感兴趣的:(javascript继承)