JavaScript中的Array(1)和新的Array(1)有什么区别? [英] What's the difference between Array(1) and new Array(1) in JavaScript?

查看:349
本文介绍了JavaScript中的Array(1)和新的Array(1)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始考虑这个问题,但是在 jsFiddle 中捣乱时,我们无法发现任何差异。

I just started thinking about this, but couldn't get any differences to expose themselves whilst mucking around in jsFiddle.

var a = new Array(1),
    b = Array(1);

console.log(a, b);

输出是两个数组,其中包含一个未定义的成员。

Output is two arrays with one undefined member.

为(in)执行显示它们具有相同的属性。

Doing a for ( in ) reveals they have the same properties.

这些之间有什么区别?第一个是否只是显式地实例化对象?

What are the differences between these? Does the first one simply instantiate the object explicitly?

请不要告诉我使用数组文字表示法,因为我已经知道了。我更希望在上面解释的知识中填补这个空白。

Please don't lecture me about using array literal notation as I already know about that. I'm more wishing to fill this gap in my knowledge explained above.

推荐答案

使用数组,两者都是等价的。当它被称为函数时, new 被注入:

With Array, both are equivalent. The new is injected when it's called as a function:


15.4 .1 数组构造函数被称为函数

15.4.1 The Array Constructor Called as a Function

数组作为函数而不是构造函数调用,它创建并初始化一个新的Array对象。因此函数调用 Array(...)等同于具有相同参数的对象创建表达式 new Array(...)

When Array is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

来自 ECMA-262,第3版(类似于第5版

这篇关于JavaScript中的Array(1)和新的Array(1)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆