🥏 JavaScript 类的静态属性和静态方法
# 静态方法
statc  关键字修饰的方法,可以把一个方法作为 一个整体 赋值给类。
🌰 例子:
class User {
  static staticMethod() {
    console.log(this === User);
  }
}
User.staticMethod(); // true
 1
2
3
4
5
6
7
2
3
4
5
6
7
跟直接将其作为属性赋值的作用相同:
class User { } User.staticMethod = function() { console.log(this === User); }; User.staticMethod(); // true1
2
3
4
5
上面的例子中,  User.staticMethod()  调用中的  this  的值是类构造器  User  自身。
通常,静态方法用于实现属于整个类,但不属于该类任何特定对象的函数。
🌰 例子:
例如,有对象  Article ,并且需要一个方法来 比较 它们。
class Article {
  constructor(title, date) {
    this.title = title;
    this.date = date;
  }
  static compare(articleA, articleB) {
    return articleA.date - articleB.date;
  }
}
let articles = [
  new Article("HTML", new Date(2019, 1, 1)),
  new Article("CSS", new Date(2019, 0, 1)),
  new Article("JavaScript", new Date(2019, 11, 1))
]
articles.sort(Article.compare);
console.log(articles[0].tilte) // CSS
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
这里
Article.compare方法代表上面的文章,意思是比较它们。它不是文章的方法,而是整个class的方法。
🌰 例子 / 「工厂方法」:
加入需要多种方法来创建一篇文章:
- 通过用给定的参数来创建( 
title,date等) - 使用今天的日期来创建一个空的文章
 - … 其他方法
 
第一种方法可以使用  constructor  实现;第二种方法可以创建一个 静态方法 实现:
class Article {
  constructor(title, date) {
    this.title = title;
    this.date = date;
  }
  static createTodays() {
    // 记住 this = Article
    return new this("Today's digest", new Date());
  }
}
let article = Article.createTodays();
alert( article.title ); // Today's digest
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
createTodays不是一个文章的方法,而是整个class的方法。
🌰 例子 / 静态方法可以用于 数据库相关 的公共类,用于搜索 / 保存 / 删除数据库中的条目:
假设  Article  是一个用来管理文章的特殊类, remove  是通过  id  移除文章的方法:
Article.remove({id: 12345});
 1
# 静态属性
静态属性类似常规的类属性,但前面加有  static :
🌰 例子:
class Article {
  static publisher = "Levi Ding";
}
alert( Article.publisher ); // Levi Ding
 1
2
3
4
5
2
3
4
5
相当于给  Article  直接赋值:
Article.publisher = "Levi Ding";
 1
# 继承 静态方法和属性
静态属性和方法是可被继承的。
🌰 例子:
class Animal {
  static planet = "Earth";
  constructor(name, speed) {
    this.speed = speed;
    this.name = name;
  }
  run(speed = 0) {
    this.speed += speed;
    alert(`${this.name} runs with speed ${this.speed}.`);
  }
  static compare(animalA, animalB) {
    return animalA.speed - animalB.speed;
  }
}
class Rabbit extends Animal {
  hide() {
    alert(`${this.name} hides!`);
  }
}
let rabbits = [
  new Rabbit("White Rabbit", 10),
  new Rabbit("Black Rabbit", 5)
];
rabbits.sort(Rabbit.compare);
console.log(rabbits) 
console.log(Rabbit.planet) // Earth
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
调用
Rabbit.compare时,继承的Animal.compare将会被调用。
实质上是利用原型继承,继承对常规方法和静态方法都有效:
alert(Rabbit.__proto__ === Animal); // true
alert(Rabbit.prototype.__proto__ === Animal.prototype); // true
 1
2
3
2
3
# 总结
- 静态方法被用于实现属于整个类的功能。它与具体的类实例无关。
 - 静态属性声明与直接给类本身赋值相同。
 - 静态属性和方法是可被继承的。
 
编辑  (opens new window)
  📢 上次更新: 2022/09/02, 10:18:16
