unicorn/new-for-builtins 严谨
作用
强制使用 new 来处理以下内置元素:Object、Array、ArrayBuffer、BigInt64Array、BigUint64Array、DataView、Date、Error、Float32Array、Float64Array、Function、Int8Array、Int16Array、Int32Array、Map、WeakMap、Set、WeakSet、Promise、RegExp、Uint8Array、Uint16Array、Uint32Array、Uint8ClampedArray、SharedArrayBuffer、Proxy、WeakRef、FinalizationRegistry。
禁止使用 new 来处理以下内置函数:String、Number、Boolean、Symbol、BigInt。
对于这些情况,不应使用 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);