书上的解释不太理解,求大神帮解释下。为什么第一种没有继承到sides属性// 创建作用域安全的构造函数function Polygon(sides) { if (this instanceof Polygon) { console.log('this', this); this.sides = sides; this.getArea = function(){ return 0; } } else { return new Polygon(sides); }}// 非作用域安全的构造函数function Rectangle(width, height) { Polygon.call(this, 2); this.width = width; this.height = height; this.getArea = function (){ return this.width * this.height; }}let rect = new Rectangle(5,10);console.log(rect.sides);为什么rect没有继承到side属性而通过原型就可以Rectangle.prototype = new Polygon();let rect = new Rectangle(2,4);console.log(rect.sides);// 2
添加回答
举报
0/150
提交
取消