没有 "this" 的方法放在哪里?
Where to put a method without "this"?
我的代码:
class DoIt {
city () {
// others operations...
const number = 5; // from operations
const amount = this.getAmount (number);
// others operations...
}
street () {
// others operations...
const number = 10; // from operations
const amount = this.getAmount (number);
// others operations...
}
getAmount (number) {
return number * 10 * 3.14 / 3; // example...
}
}
我使用 "eslint" 检查我的代码,但出现错误:
error Expected 'this' to be used by class method 'getAmount'
class-methods-use-this
那么没有"this"的方法放在哪里?这段代码对我的 class 很重要,所以我不应该创建助手 class...
也许这个方法不重要?在其他语言的 OOP 中是否可以在 class 方法中存在没有 'this' 的方法?
class-methods-use-this 是一个 eslint 检查,看看你的 class 方法是否没有引用这个。在某些情况下,通过在方法声明前添加 static 关键字使其成为静态可能更好。在其他情况下,您可以忽略此检查。
我的代码:
class DoIt {
city () {
// others operations...
const number = 5; // from operations
const amount = this.getAmount (number);
// others operations...
}
street () {
// others operations...
const number = 10; // from operations
const amount = this.getAmount (number);
// others operations...
}
getAmount (number) {
return number * 10 * 3.14 / 3; // example...
}
}
我使用 "eslint" 检查我的代码,但出现错误:
error Expected 'this' to be used by class method 'getAmount' class-methods-use-this
那么没有"this"的方法放在哪里?这段代码对我的 class 很重要,所以我不应该创建助手 class...
也许这个方法不重要?在其他语言的 OOP 中是否可以在 class 方法中存在没有 'this' 的方法?
class-methods-use-this 是一个 eslint 检查,看看你的 class 方法是否没有引用这个。在某些情况下,通过在方法声明前添加 static 关键字使其成为静态可能更好。在其他情况下,您可以忽略此检查。