ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
## 语法 ~~~ constructor([arguments]) { ... } ~~~ ## 描述 在一个类中只能有一个名为 “constructor” 的特殊方法。 一个类中出现多次构造函数 (`constructor)`方法将会抛出一个[`SyntaxError`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError "SyntaxError 对象代表尝试解析语法上不合法的代码的错误。")错误。 在一个构造方法中可以使用`super`关键字来调用一个父类的构造方法。 如果没有显式指定构造方法,则会添加默认的 constructor 方法。 如果不指定一个构造函数(constructor)方法, 则使用一个默认的构造函数(constructor)。 ## 示例 ### 使用`constructor`方法 ~~~js class Square extends Polygon { constructor(length) { // 在这里, 它调用了父类的构造函数, 并将 lengths 提供给 Polygon 的"width"和"height" super(length, length); // 注意: 在派生类中, 必须先调用 super() 才能使用 "this"。 // 忽略这个,将会导致一个引用错误。 this.name = 'Square'; } get area() { return this.height * this.width; } set area(value) { // 注意:不可使用 this.area = value // 否则会导致循环call setter方法导致爆栈 this._area = value; } } ~~~ ### 另一个例子 看看这个代码片段 ~~~js class Polygon { constructor() { this.name = "Polygon"; } } class Square extends Polygon { constructor() { super(); } } class Rectangle {} Object.setPrototypeOf(Square.prototype, Rectangle.prototype); console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true let newInstance = new Square(); console.log(newInstance.name); //Polygon ~~~ 这里,**Square**类的原型被改变,但是在正在创建一个新的正方形实例时,仍然调用前一个基类**Polygon**的构造函数。 ### 默认构造方法 如前所述,如果不指定构造方法,则使用默认构造函数。对于基类,默认构造函数是: ~~~js constructor() {} ~~~ 对于派生类,默认构造函数是: ~~~js constructor(...args) { super(...args); } ~~~