日期:2014-05-16 浏览次数:20420 次
function demo(){
var A=function(name){
this.name=name;
}
A.prototype.showName=function(){alert(this.name);}
var B=function(name, age){
A.call(this, name);
this.age=age;
}
//B.prototype=new A(); //这里2行代码的效果是一样的
B.prototype=A.prototype; //目的是返回A的"实例"的指针
B.prototype.showAge=function(){alert(this.age);}
var b=new B("david", 31);
b.showName();
b.showAge();
}
var A=function(name){
this.name=name;
//return this; 这是隐式代码, JS会自动帮我们执行, 这里是关键
}
改为
var A=function(name){
var o=new Object();
o.name=name;
return o;
}
function demo(){
var A=function(name){
this.name=name;
}
A.prototype.showName=function(){alert(this.name);} //the function return this, "this" is the pointer of instance
A.prototype.pointer=function(){return this;}
var B=function(name, age){
A.call(this, name);
this.age=age;
}
//B.prototype=new A();
//B.prototype=A.prototype;
B.prototype=new A().pointer();
B.prototype.showAge=function(){alert(this.age);}
var b=new B("david", 31);
b.showName();
b.showAge();
}
function demo(){
var A=function(name){
this.name=name;
}
A.prototype.showName=function(){alert(this.name);} //the function return this, "this" is the pointer of instance
A.prototype.pointer=function(){return this;}
A.showSex=function(){alert("male");}
A.city="shenzhen";
var B=function(name, age){
A.call(this, name);
this.age=age;
}
//B.prototype=new A();
//B.prototype=A.prototype;
B.prototype=A;
B.prototype.showAge=function(){alert(this.age);}
var b=new B("david", 31);
try {
b.showName();
} catch (exception) {
alert(exception);
}
b.showAge();
b.showSex();
alert(b.city);
}
function demo(){
var A=function(name){
this.name=name;
}
A.prototype.showName=function(){alert(this.name);} //the function return this, "this" is the pointer of instance
A.prototype.showSex=function(){alert("male");}
A.prototype.city="shenzhen";
var B=function(name, age){
A.call(this, name);
this.age=age;
}
B.prototype=A.prototype;
B.prototype.showAge=function(){alert(this.age);}
var b=new B("david", 31);
try {
alert(b.constructor); //output Class A construct
alert(b.constructor==A);//output true
} catch (exception) {
alert(exception);
}
}
function demo(){
var A=function(name){
this.name=name;
}
A.prototype.showName=function(){alert(this.name);} //the function return this, "this" is the pointer of instance
A.prototype.showSex=function(){alert("male");}
A.prototype.city