作者lausai (恨怨悲苦憎怒嗔)
看板Ajax
标题Re: [ js ] 请问 new 的语法
时间Tue Apr 28 12:10:53 2009
因为对这个问题有兴趣 就去查了一下资料
在Standard ECMA-262 有这麽一段
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 call Array(...) is
equivalent to the object creation expression new Array(...) with the same
argument
所以
t=Array();
t=new Array();
是一样的
※ 引述《B9 (叶酸酸♪)》之铭言:
: ※ 引述《HuangJC (吹笛牧童)》之铭言:
: : t = new Array(); //这样可以建立一个阵列
: : t = Array(); //这样一样可以建立阵列,有差吗?
: "The new operator must be followed by a function invocation. It creates a new
: object, with no properties and then invokes the function, passing the new
: object as the value of the this keyword."
: from: JavaScript: The Definitive Guide, 5th Edition, 9.1. Constructors
: 上面是对 new 的解释。
: 不过我没有在这本书看过 t = Array() 的看法,在我来看的话,我会看成是一个
: function,并将 return value assign 给 t。
: 可以用 for ... in statement 测试,只要 a 的 attributes b 都有,
: 而且 b 的 attributes a 也都有,就可以当作都是“一样”的 object 了。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.115.53.25
1F:推 tomin:懒人的福音 可以少打三个字加一个空格 04/28 13:31
2F:推 ybite:那跟 t = []; 一样吗? (好奇) 04/28 15:58
3F:→ lausai:恩 一样的 都是空的array 04/28 18:06
4F:推 xrancyma:那和预定阵列大小一样吗?例如:var x=Array(9) 04/29 01:57