奇怪的JavaScript赋值语法的说明

时间:2017-06-30 17:19:10

标签: javascript function object

我正在阅读一篇文章,解释JavaScript中的函数原型设计和继承,当我遇到这些代码时,我无法解决这个问题。

a = {} 
(function(){}())

这些线究竟做了什么?

1 个答案:

答案 0 :(得分:3)

a = {} // Be 'a' an empty object (But why ?)

function(){   // Declare a function that says Hello when it's called
   alert("Hello");
}

function(){   // Declare a function that says Hello and execute it immediately with ()
   alert("Hello");
}()

function(){}()   // Declare a function that...does nothing, and execute it immediately with ()... But apparently you can't, that's a syntax error (Thanks @pointy)

(function(){}()) // Makes it work (no syntax error)