Javascript 面向对象编程继承方法四(原型继承)


function Person(name,age,position){
this.name = name;
this.age = age;
this.position = position;
}

Person.prototype.displayInfo = function(){
alert("[Name: "+ this.name +"] [Age: "+this.age+"] [position: "+this.position+"]");
}

function Programmer(name,age,position){
this.tempObject = Person; //声明临时指针指向父类
this.tempObject(name,age,position);
/*
相当于调用函数Person(...),调用父类构造函数,没有new,this指向子类,
进而有:this.displayInfo = function()
相当于:this.person = function(){
this.display = function()
}
*/
delete this.tempObject; //删除临时指针,防止通过tempObject引用覆盖超类Person的属性和方法
// Person.call(this , name,age,position);
// Person.apply(this ,new Array(name,age,position));
}

Programmer.prototype = new Person();

var oBpmProgrammer = new Programmer("Kevin",24,"BPM Programmer");
var oBopReportDeveloper = new Programmer("Witkey",25,"BOP Report Developer");
oBpmProgrammer.displayInfo(); //[Name: Kevin] [Age: 24] [position: BPM Programmer]
oBopReportDeveloper.displayInfo(); //[Name: Witkey] [Age: 25] [position: BOP Report Developer]

/*
this.tempObject = Person; //声明临时指针指向父类
this.tempObject(name,age,position);
delete this.tempObject; //删除临时指针,防止通过tempObject引用覆盖超类Person的属性和方

以上三行代码从Person继承name,age,position属性,同理
Person.call(this , name,age,position); 和Person.apply(this ,new Array(name,age,position));

如果是使用以下方法,还是一样要有:programmer.prototype = new Person();
Person.call(this , name,age,position); 或Person.apply(this ,new Array(name,age,position));

programmer.prototype = new Person(); 继承原型方法或属性
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值