gpt4 book ai didi

javascript - 父对象原型(prototype)继承的差异

转载 作者:行者123 更新时间:2023-11-30 14:50:33 27 4
gpt4 key购买 nike

<分区>

我找到了 3 种方法来继承另一个 Object-constructor 的原型(prototype):

我可以同时使用这三个吗? => 如果我在 Rectangle 实例上测试 draw() 方法,它们似乎可以工作...

还有其他方法吗?

function Shape(shapeName){
this.shapeName = shapeName;
}
Shape.prototype.draw = function(){
console.log('I am a '+this.shapeName+' and I am drawing myself');
}

function Rectangle(shapeName,l,b){
Shape.apply(this,arguments);
this.lengte = l;
this.breadth = b;
}

// Possibility 1 ==> Rectangle.prototype = Object.create(Shape.prototype);
// Possibility 2 ==> Object.assign(Rectangle.prototype,Shape.prototype);
// Possibility 3 ==> Rectangle.prototype = Shape.prototype;

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com