原生JS实现类

//F是空对象,所以几乎不占内存。这时,修改SonType的prototype对象,就不会影响到ParentType的prototype对象。
var F = function(){};
F.prototype = ParentType.prototype;
SonType.prototype = new F();
SonType.prototype.constructor = SonType;
//为子对象设一个uber属性,这个属性直接指向父对象的prototype属性。(uber是一个德语词,意思是"向上"、"上一层"。)这等于在子对象上打开一条通道,可以直接调用父对象的方法。这一行放在这里,只是为了实现继承的完备性,纯属备用性质。
SonType.uber = ParentType.prototype;
function ParentType(name, age) {
    this.name = name;
    this.age = age;
};
ParentType.prototype.getParentName = function() {
    return this.name;
};
ParentType.prototype.getParentAge = function() {
    return this.age;
};
ParentType.prototype.ParentPrint = function(_that) {
   if(!_that) {
       return this.getParentName() + '-' + this.getParentAge();
   }else {
       return _that.getParentName() + '-' + _that.getParentAge();
   }
};
function SonType(name, age, sex) {
   ParentType.call(this, name, age);  //将父对象的构造函数绑定在子对象上
   this.sex = sex;
};
SonType.prototype.getSonSex = function() {
    return this.sex;
};
SonType.prototype.SonPrint = function() {
    return SonType.uber.ParentPrint(this) + '-' + this.getSonSex();
};
var son = new SonType('Jeremy', 22, '男');
son.SonPrint();  //Jeremy-22-男
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值