Index

no-new-wrappers

禁止对 StringNumberBoolean 对象使用 new 运算符

在 JavaScript 中,有三种具有封装对象的原始类型:字符串、数字和布尔值。它们分别由构造函数 StringNumberBoolean 表示。每当读取这些原始值之一时,就会使用原始封装类型,从而赋予它们类似对象的能力,例如方法。在背后,会创建一个相关封装类型的对象,然后销毁它,这就是你可以在原始值上调用方法的原因,例如:

🌐 There are three primitive types in JavaScript that have wrapper objects: string, number, and boolean. These are represented by the constructors String, Number, and Boolean, respectively. The primitive wrapper types are used whenever one of these primitive values is read, providing them with object-like capabilities such as methods. Behind the scenes, an object of the associated wrapper type is created and then destroyed, which is why you can call methods on primitive values, such as:

const text = "Hello world".substring(2);

在这个示例的幕后,构建了一个 String 对象。substring() 方法存在于 String.prototype 上,因此字符串实例可以访问它。

🌐 Behind the scenes in this example, a String object is constructed. The substring() method exists on String.prototype and so is accessible to the string instance.

也可以手动创建一个新的封装器实例:

🌐 It’s also possible to manually create a new wrapper instance:

const stringObject = new String("Hello world");
const numberObject = new Number(33);
const booleanObject = new Boolean(false);

虽然可能,但没有任何充分的理由使用这些原始封装作为构造函数。它们往往会让其他开发者感到困惑,因为它们看起来应该像原始类型一样工作,但实际上并非如此。例如:

🌐 Although possible, there aren’t any good reasons to use these primitive wrappers as constructors. They tend to confuse other developers more than anything else because they seem like they should act as primitives, but they do not. For example:

const stringObject = new String("Hello world");
console.log(typeof stringObject);       // "object"

const text = "Hello world";
console.log(typeof text);               // "string"

const booleanObject = new Boolean(false);
if (booleanObject) {    // all objects are truthy!
    console.log("This executes");
}

第一个问题是原始封装对象实际上是对象。这意味着 typeof 会返回 "object" 而不是 "string""number""boolean"。第二个问题出现在布尔对象上。每个对象都是为真值的,这意味着 Boolean 的一个实例总是解析为 true,即使它的实际值是 false

🌐 The first problem is that primitive wrapper objects are, in fact, objects. That means typeof will return "object" instead of "string", "number", or "boolean". The second problem comes with boolean objects. Every object is truthy, that means an instance of Boolean always resolves to true even when its actual value is false.

出于这些原因,通常认为最好避免在 new 中使用原始封装类型。

🌐 For these reasons, it’s considered a best practice to avoid using primitive wrapper types with new.

规则详情

🌐 Rule Details

该规则旨在消除在 new 运算符中使用 StringNumberBoolean。因此,每当它看到 new Stringnew Numbernew Boolean 时都会发出警告。

🌐 This rule aims to eliminate the use of String, Number, and Boolean with the new operator. As such, it warns whenever it sees new String, new Number, or new Boolean.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-new-wrappers: "error"*/

const stringObject = new String("Hello world");
const numberObject = new Number(33);
const booleanObject = new Boolean(false);

const stringObject2 = new String;
const numberObject2 = new Number;
const booleanObject2 = new Boolean;

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-new-wrappers: "error"*/

const text = String(someValue);
const num = Number(someValue);

const object = new MyString();

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 When Not To Use It

如果你想允许使用原始封装对象,那么你可以安全地禁用此规则。

🌐 If you want to allow the use of primitive wrapper objects, then you can safely disable this rule.

版本

此规则是在 ESLint v0.0.6 中引入。

进阶读物

资源