Friend.prototype.constructor = Friend
原先Friend.prototype.constructor指向的是Person
但是感觉并没有什么用
有没有弹出的结果都一样,感觉都很好完成了继承
function Person(name,age){
this.name = name;
this.age = age;
if(typeof this.sayName != 'function'){
Person.prototype.sayName = function(){
alert(this.name);
}
}
}
var per1 = new Person('zhang',23);
var per2 = new Person('wagn',23);
function Friend(name,age,sex){
Person.call(this,name,age);
this.sex = sex;
}
Friend.prototype = new Person();
Friend.prototype.constructor = Friend; //不斧正时,constructor指向Person
Friend.prototype.saySex=function(){
alert(this.sex);
}
var fri1 = new Friend('wang','11','nan');
var fri2 = new Friend('li','55','nv');
alert(Person.prototype.constructor);
繁花如伊
相关分类