Home  >  Article  >  Web Front-end  >  JavaScript自执行闭包的小例子_javascript技巧

JavaScript自执行闭包的小例子_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:30:50934browse
复制代码 代码如下:
 
 //无参数的 
 (function(){ 
    alert('hello'); 
 })(); 

 //有参数的 
 (function(name){ 
    alert('Hello ' + name); 
 })('world');

    这个闭包有什么用呢?一眼望去就是括号围着,局部变量想跑也不出来到处捣乱,这是其中一个主要用途,当然了,封装一些javascript库也离不开它。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn