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:

var 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:

var stringObject = new String("Hello world");
var numberObject = new Number(33);
var 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:

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

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

var 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"*/

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

var stringObject = new String;
var numberObject = new Number;
var booleanObject = new Boolean;

此规则的正确代码示例:

¥Examples of correct code for this rule:

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

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

var object = new MyString();

何时不使用

¥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 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号