Re: [ js ] 請問 new 的語法
因為對這個問題有興趣 就去查了一下資料
在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
推
04/28 13:31, , 1F
04/28 13:31, 1F
推
04/28 15:58, , 2F
04/28 15:58, 2F
→
04/28 18:06, , 3F
04/28 18:06, 3F
推
04/29 01:57, , 4F
04/29 01:57, 4F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):