function Shape(){} Shape.prototype.name = 'shape'; Shape.prototype.toString = function () { return this.name; }; function TwoDShape(){} var F = function () {}; F.prototype = Shape.prototype; TwoDShape.prototype = new F(); TwoDShape.prototype.constructor = TwoDShape; TwoDShape.prototype.name = '2D shape'; function Triangle(side,height) { this.side = side; this.height = height; } var F = function(){}; F.prototype = TwoDShape.prototype; Triangle.prototype = new F(); Triangle.prototype.constructor = Triangle; Triangle.prototype.name = 'Triangle'; Triangle.prototype.getArea = function () { return this.side * this.height/2; }; var my = new Triangle(5,10); my.getArea(); //输出 25 my.toString(); //输出 "Triangle" //!!!!!!! 通过这种方法,我们就可以在保持原型链的基础上使父对象的属性摆脱子对象的影响了: my.__proto__.__proto__.__proto__.constructor; //输出 Shape() var s = new Shape(); s.name; //输出 “shape”
2.1解决只继承原型的副作用(临时构造器)
最新推荐文章于 2025-09-16 06:30:00 发布
本文通过具体示例展示了JavaScript中如何实现原型链继承,并保持父对象属性不受子对象影响的方法。涉及构造函数、原型属性及方法定义。
427

被折叠的 条评论
为什么被折叠?



