書上的解釋不太理解,求大神幫解釋下。為什么第一種沒有繼承到sides屬性// 創(chuàng)建作用域安全的構(gòu)造函數(shù)function Polygon(sides) { if (this instanceof Polygon) { console.log('this', this); this.sides = sides; this.getArea = function(){ return 0; } } else { return new Polygon(sides); }}// 非作用域安全的構(gòu)造函數(shù)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
javascript高級程序設(shè)計(jì) 安全作用域構(gòu)造函數(shù) 問題
慕碼人8056858
2019-02-20 17:18:17