eslint/no-new-native-nonconstructor 正確性
功能說明
不允許對全域非建構函式(Symbol
、BigInt
)使用 new
運算子。
為何這是不好的?
new Symbol
和 new BigInt
都會拋出型別錯誤,因為它們是函式而不是類別。很容易因為假設大寫字母表示類別而犯此錯誤。
範例
此規則的不正確程式碼範例
js
// throws a TypeError
let foo = new Symbol("foo");
// throws a TypeError
let result = new BigInt(9007199254740991);
此規則的正確程式碼範例
js
let foo = Symbol("foo");
let result = BigInt(9007199254740991);