作者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