Bản dịch này chưa hoàn thành. Xin hãy giúp dịch bài viết này từ tiếng Anh
Phương thức constructor là một phương thức đặc biệt dùng để khởi tạo 1 object và được tạo ở trong class.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Cú pháp
constructor([arguments]) { ... }
Mô tả
Chỉ có duy nhất 1 phương thức đặc biệt tên là "constructor" ở trong class. Có nhiều hơn 1 phương thức constructor ở trong class thì sẽ gây ra lỗiSyntaxError.
Một constructor có thể sử dụng từ khóa super để gọi đến constructor của class cha.
Nếu bạn không chỉ định 1 phương thức constructor thì constructor mặc định sẽ được sử dụng
Ví dụ
Sử dụng phương thức constructor
Đoạn code này được lấy từ classes sample (live demo).
class Square extends Polygon {
constructor(length) {
// Here, it calls the parent class' constructor with lengths
// provided for the Polygon's width and height
super(length, length);
// Note: In derived classes, super() must be called before you
// can use 'this'. Leaving this out will cause a reference error.
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
set area(value) {
this.area = value;
}
}
Ví dụ khác
Hãy xem đoạn code sau
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
Ở đây prototype của class Square đã bị thay đổi nhưng constructor kế thừa từ class Polygon vẫn được gọi khi tạo ra 1 thực thể mới.
Default constructors
Như đã nói ởi trước, nếu bạn không chỉ đỉnh 1 phương thức constructor thì default constructor sẽ được sử dụng. Với những class cơ bản thì default contructor sẽ là:
constructor() {}
Với những class dẫn xuất, default constructor sẽ là:
constructor(...args) {
super(...args);
}
Thông số kĩ thuật
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Constructor Method' in that specification. |
Standard | Initial definition. |
| ECMAScript (ECMA-262) The definition of 'Constructor Method' in that specification. |
Living Standard |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
constructor | Chrome
Full support
49
| Edge Full support 13 | Firefox Full support 45 | IE No support No | Opera
Full support
36
| Safari Full support 9 | WebView Android
Full support
49
| Chrome Android
Full support
49
| Firefox Android Full support 45 | Opera Android
Full support
36
| Safari iOS Full support 9 | Samsung Internet Android Full support 5.0 | nodejs
Full support
6.0.0
|
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.