长风破浪会有时
直挂云帆济沧海

js实现一个继承

function Parent(name) {
    this.name = name;
}
Parent.prototype.sayName = function() {
    console.log('parent name:', this.name);
}
function Child(name, parentName) {
    Parent.call(this, parentName);  
    this.name = name;    
}
function create(proto) {    
    function F(){}
    F.prototype = proto;    
    return new F();
}
Child.prototype = create(Parent.prototype);
Child.prototype.sayName = function() {
    console.log('child name:', this.name);
}
Child.prototype.constructor = Child;

var parent = new Parent('father');
parent.sayName();    // parent name: father

var child = new Child('son', 'father');
赞(1) 打赏
未经允许不得转载:阿锋哥前端开发经验分享 » js实现一个继承
分享到: 更多 (0)

几百块就能做营销型企业网站?

查看详情

可怜可怜作者吧

支付宝扫一扫打赏

微信扫一扫打赏