js继承摘要

对象的构造函数是指向创建对象的类的原型对象的构造函数。

类是一个Function, Function都有原型对象,原型对象的构造函数指向类的声明。

function Person(){

}

Person.prototype.constructor === Person //true

var p1 = new Person();

p1.constructor === Person  //true

 

a.prototype = {}  等价于 a.prototype = new object({});

此时 a.prototype.constructor 指向错误, 指到了object上

应该修正: a.prototype.constructor = a

原型继承typescript代码:

 

class Person {
    constructor(private name: string) {

    }

    getName() {
        return this.name;
    }
}

class Employee extends Person {
    constructor(name: string, private age: number) {
        super(name);
    }

    getAge() {
        return this.age;
    }
}

 

对应的js代码:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b)
        if (b.hasOwnProperty(p))
            d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Person = (function () {
    function Person(name) {
        this.name = name;
    }
    Person.prototype.getName = function () {
        return this.name;
    };
    return Person;
}());
var Employee = (function (_super) {
    __extends(Employee, _super);
    function Employee(name, age) {
        _super.call(this, name);
        this.age = age;
    }
    Employee.prototype.getAge = function () {
        return this.age;
    };
    return Employee;
}(Person));

 

转载于:https://www.cnblogs.com/zq8024/p/6178235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值