Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我经常像这样编写我的 JS 自我执行匿名函数
(function(){})()
但前几天我在某人的代码中看到了这个
(function(){}())
有什么区别,推荐一个而不是另一个?
(function(){}());
我推荐这个,因为它更有意义。
你有你的函数function(){},然后你追加()执行它,然后你把整个东西包装进去()以指定它是一个表达式。这样做是为了让 js 解释器不会将其定义为函数声明,而是定义为函数表达式。
function(){}
()
不过没关系,它会正确执行,所以这是个人品味问题。