Skip to content

noClassAssign

诊断类别:lint/suspicious/noClassAssign

¥Diagnostic Category: lint/suspicious/noClassAssign

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止重新分配类成员。

¥Disallow reassigning class members.

类声明创建了一个我们可以修改的变量,但是,在大多数情况下,修改是一个错误。

¥A class declaration creates a variable that we can modify, however, the modification is a mistake in most cases.

¥Examples

¥Invalid

class A {}
A = 0;
code-block.js:2:1 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

1 │ class A {}
> 2 │ A = 0;
^
3 │

‘A’ is defined here.

> 1 │ class A {}
^
2 │ A = 0;
3 │

A = 0;
class A {}
code-block.js:1:1 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

> 1 │ A = 0;
^
2 │ class A {}
3 │

‘A’ is defined here.

1 │ A = 0;
> 2 │ class A {}
^
3 │

class A {
b() {
A = 0;
}
}
code-block.js:3:3 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

1 │ class A {
2 │ b() {
> 3 │ A = 0;
^
4 │ }
5 │ }

‘A’ is defined here.

> 1 │ class A {
^
2 │ b() {
3 │ A = 0;

let A = class A {
b() {
A = 0;
// `let A` is shadowed by the class name.
}
}
code-block.js:3:3 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

1 │ let A = class A {
2 │ b() {
> 3 │ A = 0;
^
4 │ // let A is shadowed by the class name.
5 │ }

‘A’ is defined here.

> 1 │ let A = class A {
^
2 │ b() {
3 │ A = 0;

¥Valid

let A = class A {}
A = 0; // A is a variable.
let A = class {
b() {
A = 0; // A is a variable.
}
}
class A {
b(A) {
A = 0; // A is a parameter.
}
}

¥Related links