为什么 people[1].a 无法访问 class User 的属性?
Why people[1].a don't have access to properties from class User?
为什么人们[1].a 无法访问 class 用户的属性,但需要通过 proto 进行嵌套?!
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
static a = 12;
}
class User2 extends User {}
let people = [
new User2('Vasia', 20),
new User2('Peter', 19),
new User2('Misha', 21),
]
console.dir(User2.a) // 12
console.dir(people[1].a) // ?????
static
属性出现在 Class 对象上。它们没有出现在 class 的 个实例 上。
它们旨在容纳静态方法。
来自MDN:
The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.
为什么人们[1].a 无法访问 class 用户的属性,但需要通过 proto 进行嵌套?!
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
static a = 12;
}
class User2 extends User {}
let people = [
new User2('Vasia', 20),
new User2('Peter', 19),
new User2('Misha', 21),
]
console.dir(User2.a) // 12
console.dir(people[1].a) // ?????
static
属性出现在 Class 对象上。它们没有出现在 class 的 个实例 上。
它们旨在容纳静态方法。
来自MDN:
The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.