function Parent(name){
if(name){
this.name = name;
}
else{
this.nameless = true;
}
}
Parent.prototype.sayName = function(){
console.info(this.name);
}
Parent.prototype.hasName = function(){
console.info(this.nameless?"无名":"有");
}
function Son(){
Parent.apply(this,arguments);
//other code
}
Son.prototype = Parent.prototype;
Son.prototype.constructor = Son;
function Parent(name){
if(name){
this.name = name;
}
else{
this.nameless = true;
}
}
Parent.prototype.sayName = function(){
console.info(this.name);
}
Parent.prototype.hasName = function(){
console.info(this.nameless?"无名":"有");
}
function Son(){
Parent.apply(this,arguments);
//other code
}
Son.prototype = new Parent();
Son.prototype.constructor = Son;
var man = new Son("张三");
man.sayName();
man.hasName();
function Parent(name){
if(name){
this.name = name;
}
else{
this.nameless = true;
}
}
Parent.prototype.sayName = function(){
console.info(this.name);
}
Parent.prototype.hasName = function(){
console.info(this.nameless?"无名":"有");
}
function Son(){
Parent.apply(this,arguments);//在这调用父类构造函数构造该类成员变量。
}
function tempCtor() {}; //中间类
tempCtor.prototype = Parent.prototype; //中间类继承父类的成员方法,而不继承父类的成员变量
Son.prototype = new tempCtor();
Son.prototype.constructor = Son;
var man = new Son("张三");
man.sayName();
man.hasName();