简体   繁体   English

为什么使用{}代替新的Object()并使用[]而不是new Array()和true / false而不是new Boolean()?

[英]Why use {} instead of new Object() and use [] instead of new Array() and true/false instead of new Boolean()?

Many people say that you should avoid new Object, new Array()and instead use {}. 很多人说你应该避免使用新的Object,新的Array()而是使用{}。 [], and true/false. []和真/假。

What are the benefits of using the literal constructs to get a new instance of an Object or Array rather than using new? 使用文字结构获取Object或Array的新实例而不是使用new的好处是什么? I konw that Crockford doesn't like new but is that the main argument? 我知道克罗克福德不喜欢新的,但这是主要论点吗?

The advantages of object and array literals over using the respective constructors are: 使用相应构造函数的对象和数组文字的优点是:

  • Shorter and more readable 更短,更易读
  • Safer: literals will still work when the Array or Object constructors have been overridden 更安全:当重写ArrayObject构造函数时,文字仍然有效
  • Possibly faster, though it's unlikely to be a major consideration (any bottlenecks will almost certainly originate elsewhere in code) 可能更快,但它不太可能是一个主要的考虑因素(任何瓶颈几乎肯定会源自代码中的其他地方)

In the case of arrays, there's an additional advantage of a literal: it's impossible to create an array with a single member using the Array constructor alone. 在数组的情况下,还有一个文字的额外优点:单独使用Array构造函数创建一个包含单个成员的数组是不可能的。 For example, [3] will create an array with one element which is the number 3, while new Array(3) creates an array of length 3. 例如, [3]将创建一个数组,其中一个元素是数字3,而new Array(3)创建一个长度为3的数组。

Update: the following paragraph is no longer relevant now the question has been edited. 更新:现在已经编辑了以下段落不再相关的问题。

Regarding Booleans, you seem to have a misconception: new Boolean(false) is not the same as false . 关于布尔值,你似乎有一个误解: new Boolean(false)false The Boolean() constructor creates a Boolean object whereas false and true are Boolean primitives. Boolean()构造函数创建一个Boolean对象,而falsetrue是Boolean基元。 In fact, new Boolean(false) evaluates to true when coerced into a Boolean in, for example, an if statement. 实际上,在强制转换为布尔值(例如if语句new Boolean(false)时, new Boolean(false)计算结果为true In short, there's very rarely a reason to use the Boolean() constructor. 简而言之,使用Boolean()构造函数的原因很少。 Use true and false instead. 请改用truefalse Similarly, other primitives such as strings and numbers have corresponding String() and Number() constructors that produce String and Number objects that are different to primitive strings and numbers and should generally be avoided. 类似地,其他基元(如字符串和数字)具有相应的String()Number()构造函数,这些构造函数生成与原始字符串和数字不同的StringNumber对象,通常应该避免使用。

For example, if you want to do this: 例如,如果要执行此操作:

{name:"bla",address:"address"}

the new Object() way would be: 新的Object()方式是:

var o = new Object();
o.name="bla";
o.address="address";

The first one is much shorter. 第一个要短得多。 And I think that it would be faster in many browsers, too ( jsperf testcase ). 而且我认为它在许多浏览器中也会更快( jsperf testcase )。

I think it's mostly about succinctness. 我认为这主要是关于简洁。

Why write new Array() when you can write [] , or new Object() when you can write {} ? 为什么在编写[]时写new Array() ,或者在写{}时写new Object()

Also new Boolean() is entirely redundant. new Boolean()也是完全冗余的。 A boolean is always going to need to be either true or false, so you should definitely use the built in constants for that. 布尔值总是需要为true或false,所以你绝对应该使用内置常量。

In most cases an object literal or array literal is sufficient and easier to use. 在大多数情况下, 对象文字或数组文字就足够且易于使用。 You can even call or apply prototype methods (eg [].prototype.slice.call(someObj)) It's mostly faster too. 你甚至可以调用或应用原型方法(例如[] .prototype.slice.call(someObj))它也更快 You'll may want to notice that {} and []'s constructor cannot be overwritten and that Object and Array are constructors where {} and [] are instances. 您可能需要注意{}和[]的构造函数不能被覆盖,而ObjectArray是{}和[]是实例的构造函数。

One reason not yet mentioned is the ambiguity when passing parameters to the Array constructor. 尚未提及的一个原因是将参数传递给Array构造函数时的歧义。 It's all specified behavior, it's just quirky. 这是所有指定的行为,它只是古怪。

new Array(1,2,3); // [1,2,3], that's OK
new Array(1); // [undefined], some may expect to get [1] instead

A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number. 使用给定元素初始化JavaScript数组,除了将单个参数传递给Array构造函数并且该参数是数字的情况。 (See below.) Note that this special case only applies to JavaScript arrays created with the Array constructor, not array literals created with the bracket syntax. (请参阅下文。)请注意,此特殊情况仅适用于使用Array构造函数创建的JavaScript数组,而不适用于使用括号语法创建的数组文字。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM