9. Class
构造函数尽可能使用 Class 的形式
- // 例子 9-1
- class Foo {
- static bar () {
- this.baz();
- }
- static baz () {
- console.log('hello');
- }
- baz () {
- console.log('world');
- }
- }
- Foo.bar(); // hello
- // 例子 9-2
- class Shape {
- constructor(width, height) {
- this._width = width;
- this._height = height;
- }
- get area() {
- return this._width * this._height;
- }
- }
- const square = new Shape(10, 10);
- console.log(square.area); // 100
- console.log(square._width); // 10