3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個贊
作為特例,對于 new
如果函數(shù)調(diào)用中沒有參數(shù),JavaScript只允許省略括號,從而簡化了語法。下面是使用 new
操作員: o = new Object; // Optional parenthesis omitted hered = new Date(); ...
Missing '()' invoking a constructor
1

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個贊
new Date().toString()
工作正常,并返回當(dāng)前日期。 new Date.toString()
拋出“ TypeError:Date.toString不是構(gòu)造函數(shù)"
new Date()
new Date
╔════════════╦═════════════════════════════╦═══════════════╦═════════════╗
║ Precedence ║ Operator type ║ Associativity ║ Operators ║
╠════════════╬═════════════════════════════╬═══════════════╬═════════════╣
║ 18 ║ Member Access ║ left-to-right ║ … . … ║
║ ║ Computed Member Access ║ left-to-right ║ … [ … ] ║
║ ║ new (with argument list) ║ n/a ║ new … ( … ) ║
╠════════════╬═════════════════════════════╬═══════════════╬═════════════╣
║ 17 ║ Function Call ║ left-to-right ║ … ( … ) ║
║ ║ new (without argument list) ║ right-to-left ║ new … ║
╚════════════╩═════════════════════════════╩═══════════════╩═════════════╝
從本表中可以看出:
new Foo()
比 new Foo
new Foo()
具有與 .
操作者 new Foo
具有較低優(yōu)先級的級別。 .
操作者 new Date().toString()
因?yàn)樗挠?jì)算值為 (new Date()).toString()
new Date.toString()
拋出“ TypeError:Date.toString不是構(gòu)造函數(shù)“因?yàn)?/trans> .
比 new Date
(并高于“函數(shù)調(diào)用”),表達(dá)式的計(jì)算結(jié)果為 (new (Date.toString))()
同樣的邏輯可以應(yīng)用于 … [ … ]
接線員。 new Foo
有 從右到左結(jié)合性 new Foo()
“聯(lián)想性”不適用。我認(rèn)為在實(shí)踐中這沒有任何區(qū)別。有關(guān)更多信息,請參見 這,這個 所以問題
一個比另一個更好嗎?
new Foo()

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個贊
var someVar = myFunc; // this assigns the function myFunc to someVarvar someOtherVar = myFunc(); // this executes myFunc and assigns the returned value to someOtherVar
添加回答
舉報(bào)