你如何用JavaScript解释这个结构? [英] How do you explain this structure in JavaScript?

查看:117
本文介绍了你如何用JavaScript解释这个结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (function()
{
// codehere
}
)();

这种语法有什么特别之处?
()();暗示?

解决方案

创建一个匿名函数,闭包和全部,final()告诉它执行自己。 p>

它基本相同:

 函数名(){。 ..} 
name();

所以基本上没有什么特别的这个代码,它只是一个捷径创建一个方法和调用它而不必命名它。



这也意味着该函数是一个关闭的,或者一个对象上的内部函数,并且在需要关闭的功能时最有用。 p>

(function() 
 {
     //codehere
 }
)();

What is special about this kind of syntax? What does ()(); imply?

解决方案

The creates an anonymous function, closure and all, and the final () tells it to execute itself.

It is basically the same as:

function name (){...}
name();

So basically there is nothing special about this code, it just a 'shortcut' to creating a method and invoking it without having to name it.

This also implies that the function is a one off, or an internal function on an object, and is most useful when you need to the features of a closure.

这篇关于你如何用JavaScript解释这个结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆