如果在Javascript中声明没有New的数组会发生什么? [英] What happens if you declare an array without New in Javascript?
问题描述
可能重复:
JavaScript中的Array(1)和新的Array(1)有什么区别?
在javascript中,这两者有何不同?
var arr = Array();
var arr2 = new Array();
如果根据JS标准它们是相同的,是否有任何浏览器以不同方式对待这两种方式?<根据 ECMA-262版本5.1 :
15.4.1作为函数调用的数组构造函数
当
Array
作为函数而不是构造函数调用时,它会创建
并初始化一个新的Array对象。
因此函数调用Array(...)
等同于对象创建
表达式new Array(... )
具有相同的参数。
11.1.4 部分(它很长,所以我不会引用它)还声明数组文字直接对应于新数组(...)
构造函数调用。
Possible Duplicate:
What's the difference between Array(1) and new Array(1) in JavaScript?
In javascript, how are these two different?
var arr = Array();
var arr2 = new Array();
If they are the same according to JS standards, are there any browsers that treat both ways differently?
According to ECMA-262 edition 5.1:
15.4.1 The Array Constructor Called as a Function
When
Array
is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function callArray(...)
is equivalent to the object creation expressionnew Array(...)
with the same arguments.
The section 11.1.4 (it is quite long, so I won't quote it) also states that array literal directly corresponds to the new Array(...)
constructor call.
这篇关于如果在Javascript中声明没有New的数组会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!