跳至內容

unicorn/new-for-builtins 嚴格

作用

強制以下內建函數使用 newObjectArrayArrayBufferBigInt64ArrayBigUint64ArrayDataViewDateErrorFloat32ArrayFloat64ArrayFunctionInt8ArrayInt16ArrayInt32ArrayMapWeakMapSetWeakSetPromiseRegExpUint8ArrayUint16ArrayUint32ArrayUint8ClampedArraySharedArrayBufferProxyWeakRefFinalizationRegistry

禁止以下內建函數使用 newStringNumberBooleanSymbolBigInt

這些不應使用 new,因為那會為原始值建立物件包裝器,這不是您想要的。然而,在不使用 new 的情況下,它們可以用於將值強制轉換為該類型。

為什麼這樣不好?

它們的作用相同,但為了與其他建構子保持一致,應優先使用 new

範例

此規則的不正確程式碼範例

javascript
const foo = new String("hello world");
const bar = Array(1, 2, 3);

此規則的正確程式碼範例

javascript
const foo = String("hello world");
const bar = new Array(1, 2, 3);

參考資料

根據 MIT 許可發布。