Reference: http://segmentfault.com/a/1190000000348228
http://segmentfault.com/q/1010000000442042
Problem:
(function() {a() {alert ("a");}}) ();
Here (function () {XXX}) (); What do you mean, why it's so good to write?
For:
function foo() {...} // 这是定义,Declaration;定义只是让解释器知道其存在,但是不会运行。foo(); // 这是语句,Statement;解释器遇到语句是会运行它的。
(1)标准写法
JS Code
- (function (window, document, undefined) {
- //
- }) (window, document);
(2)作用域Scope
JavaScript有function作用域,所以function首先创建一个私有的作用域,在执行的时候都会创建一个执行上下文。
JS Code
- (function (window, document, undefined) {
- var name = ' Zhang San ';
- }) (window, document);
- Console.log (name); //Because the scope is different, the name here is undefined.
调用方法一:
JS Code
- var logmyname = Function (name) {
- Console.log (name);
- };
- Logmyname (' John Doe ');
调用方法二:
JS Code
- var logmyname = (function (name) {
- Console.log (name);
- }) (' Harry ');
需要注意的是需要用括号把函数内容括起来:
JS Code
- function () {
- // ...
- })();
没有括号的话会报语法错:
JS Code
- function () {
- // ...
- }();
也可以强制JavaScript识别代码(一般很少这么用):
JS Code
- ! function () {
- // ...
- } ();
-
- +function () {
- // ...
- } ();
-
- -function () {
- // ...
- } ();
-
- ~function () {
- < span class= "comment" >// ...
- } ();
比如:
JS Code
- var a = ! function () {
- return true;
- } ();
- Console.log (a); // print output false
-
- var b = (function () {
- return true;
- }) (
- Console.log (b); // print output true
(3)参数Arguments
传递参数给IIFE
JS Code
- (function (window) {
- //Can be called to window here
- }) (window);
- (function (window, document) {
- //This can be called to window and document
- }) (window, document);
undefined参数
在ECMAScript 3中undefined是mutable的,这意味着可以给undefined赋值,而在ECMASCript 5的strict模式(‘use strict‘;)下是不可以的,解析式时会报语法错。
所以为了保护IIFE的变量需要undefined参数:
JS Code
- (function (window, document, undefined) {
- // ...
- }) (window, document);
即使有人给undefined赋值也没有关系:
JS Code
- undefined = true;
- (function (window, document, undefined) {
- //undefined points to a local undefined variable
- }) (window, document);
(4)代码压缩Minifying
JS Code
- (function (window, document, undefined) {
- Console.log (window); //Window object
- }) (window, document);
代码压缩后,undefined的参数不再存在,但是由于 (window, document); 的调用没有传递第三个参数,所有c依然是一个本地undefined变量,所有参数中undefined的名字是无所谓什么的,只需要知道他指向的是一个undefined变量。
JS Code
- (function (A, B, c) {
- Console.log (a); //Window object
- }) (window, document);
除undefined变量外,其他的所有希望只在函数内部有效的变量都可以通过参数传递进去,比如以下的jQuery对象。
JS Code
- (function ($, window , document, undefined) {
- // Use $ to point to jquery, for example:
- // $ ( Document). addclass (' test ');
- }) (Jquery, window, document);
-
- (function (a, b, c, d) {
- // code will be compressed to:
- // a (c). addclass (' test ');
- }) (Jquery, window, document);
(function () {}) This is an expression
(function () {
var r, url;
var s = location.href;
url =/http:\/\/www.csdn.com.cn/i;
if (S.search (URL) >=0) {
var Head=document.getelementbyid ("Tophead");
Head.style.width= "990px";
}
})
An anonymous function was declared
Add a () to execute it
That is, you do not need a method name to directly create a call
function
a(){alert()}
a()
//执行该方法
function
{
return
alert()}()
//直接执行该方法
JS closure anonymous function JavaScript iife (Instant execution method) immediately-invoked function expression