no-new-wrappers
禁止带有 String
、Number
和 Boolean
对象的 new
运算符
JavaScript 中有三种具有包装对象的原始类型:字符串、数字和布尔值。它们分别由构造函数 String
、Number
和 Boolean
表示。每当读取这些原始值之一时都会使用原始包装器类型,从而为它们提供类似对象的功能,例如方法。在幕后,相关包装类型的对象被创建然后销毁,这就是为什么您可以调用原始值的方法,例如:
var text = "Hello world".substring(2);
在本示例的幕后,构造了一个 String
对象。substring()
方法存在于 String.prototype
上,因此字符串实例可以访问。
也可以手动创建一个新的包装器实例:
var stringObject = new String("Hello world");
var numberObject = new Number(33);
var booleanObject = new Boolean(false);
尽管可能,但没有任何充分的理由将这些原始包装器用作构造函数。它们比其他任何东西都更容易使其他开发人员感到困惑,因为它们似乎应该充当原语,但实际上并非如此。例如:
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");
}
MsX3wnvfKfGjTm4UmkovdDNHXcBV58nbLPCjuhm0UgMaIhb57QnmNJKBnc2KJT0vhcHhFpOZiZy35ryePH0/xFtIhhJ9sW+4Df20W/PaDm9db7B3HzNhc83MQFO9M7K926bpYo9/4KYn3l3hQSqBySEaLiuyQ9MtYBKNpt1HEtr9jhuVPBidZMYNoX+gs98xPzHQiaNjt3XnXHXzNNjGVEUNRA+GW2TiSPqmXR3AAOS4o2EZf7r4VG9abXJOnqF8by4S0wC1Id2FwaXLo9Hv2EB6deiaaSmCaObAu1lC9Cf4mPTwk/PEzu6Z79+M38lgDA5UE2YrlumYtRbYARuSiOaAeGKO9XHI5BElbbIyY17EBPd0binbh13Lh/DN7+qRN2VlPs7LOz+SPR5de8huAvvOIn+022GFPewbqQ8M+7y1ocQWwUMFcPZ4tqucyypvR+mIllWE7/axBmlv8VsHx6nO7BIuccwQZiBniNGpDHhArdqac9KNr8GdTVikYrnT2wTEkeJPvz5y5wdtRtZP0Q==
hf+AoPOPJeHVxUdl70Qed/vSfWnmxNq6KfIkdwUT3ZIWtRZ96e3QLBGvSMwTCtAd4qrrwU2BMKhdC8OU0UlBkYxvtS+T97qKGm5t0VTtuZVkF4FK7YDRLOXd3CHlYH6V
规则详情
E3TfxtxhIG1BZvvGEak4kFqbWIR5o0hShvkvxcp1rTYxlhuyNg+UXBvG+L9Z75Bpdbg3s5MgOq4LJC2yRjqlNR9vAExt1mgWLm7o19PKo52a2ccR1EbXNLmRzhz9Xki4IqO4zVdvM8J0SmabVSsgMNLfZ5/BoLuPB0e5Lez2x1x7rXSBZbB0mUAsJrnHN2W6md8bp3EsZxFe96zq0aHHTyH1UkZxUIMIcx7oAHVqLKQmL0Svf5bbWbBnXYAfKC4FkfL27UfU+ymi0KkePtwVIjExAdE95H6rRWoZCV3WJJKSwxDiFCV6YxjG+swP+SOYhbJSmdA0a5acpoeNbPYmOhk4Y3W9pjOzacorBVcu6L4=
KIjVZ8jbyN1kWo441FXYIh3AmcQhCDgs/8OlFVGexGn4OCCKsdfcgNWXdLQQbb4u
/*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;
AoqXtTV6Q8irP1mRQNh34rATVScSSkZAU175f14ev6145njeerHH1txMZ4OQcdup
/*eslint no-new-wrappers: "error"*/
var text = String(someValue);
var num = Number(someValue);
var object = new MyString();
何时不使用
1mPjiUlKLciwKxuoEjsKHOEXXrABWi8BgRTzKlRVm6S92T6M1SjajGvAbbCptxEyn/+8habYx5dcSTLC8syt587UETQRIrZAk8StwGbqT8xSnNSgk5ZeOYo+GizkqtzM