码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript---Immediately-Invoked Function Expression (IIFE)立即执行的函数表达式

时间:2014-10-13 14:43:09      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   java   for   strong   

1.一下是几种形式的函数调用:

各种调用的效率:在这编文章中有谈到:

http://suqing.iteye.com/blog/1981591

// Either of the following two patterns can be used to immediately invoke
// a function expression, utilizing the function‘s execution context to
// create "privacy."

(function(){ /* code */ }()); // Crockford recommends this one
(function(){ /* code */ })(); // But this one works just as well

// Because the point of the parens or coercing operators is to disambiguate
// between function expressions and function declarations, they can be
// omitted when the parser already expects an expression (but please see the
// "important note" below).

var i = function(){ return 10; }();
true && function(){ /* code */ }();
0, function(){ /* code */ }();

// If you don‘t care about the return value, or the possibility of making
// your code slightly harder to read, you can save a byte by just prefixing
// the function with a unary operator.

!function(){ /* code */ }();
~function(){ /* code */ }();
-function(){ /* code */ }();
+function(){ /* code */ }();

// Here‘s another variation, from @kuvos - I‘m not sure of the performance
// implications, if any, of using the `new` keyword, but it works.
// http://twitter.com/kuvos/status/18209252090847232

new function(){ /* code */ }
new function(){ /* code */ }() // Only need parens if passing arguments

 

2.What’s wrong with “Self-executing anonymous function?”

这种即使调用的函数表达式(IIFE),并不一定是匿名的,所以 JavaScript community members这个别称“Self-executing anonymous function”有令人误解的地方。

// This is a self-executing function. It‘s a function that executes (or
// invokes) itself, recursively:

function foo() { foo(); }

// This is a self-executing anonymous function. Because it has no
// identifier, it must use the  the `arguments.callee` property (which
// specifies the currently executing function) to execute itself.

var foo = function() { arguments.callee(); };

// This *might* be a self-executing anonymous function, but only while the
// `foo` identifier actually references it. If you were to change `foo` to
// something else, you‘d have a "used-to-self-execute" anonymous function.

var foo = function() { foo(); };

// Some people call this a "self-executing anonymous function" even though
// it‘s not self-executing, because it doesn‘t invoke itself. It is
// immediately invoked, however.

(function(){ /* code */ }());

// Adding an identifier to a function expression (thus creating a named
// function expression) can be extremely helpful when debugging. Once named,
// however, the function is no longer anonymous.

(function foo(){ /* code */ }());

// IIFEs can also be self-executing, although this is, perhaps, not the most
// useful pattern.

(function(){ arguments.callee(); }());
(function foo(){ foo(); }());

// One last thing to note: this will cause an error in BlackBerry 5, because
// inside a named function expression, that name is undefined. Awesome, huh?

(function foo(){ foo(); }());

3.A final aside: The Module Pattern

javascript的模块实现模式,是返回一个Object而不是函数。模块化有相关的方法和属性放在一个命名空间里,组织好整个模块的代码,降低了全局性变量的污染。

下面是一个例子:

 

// Create an anonymous function expression that gets invoked immediately,
// and assign its *return value* to a variable. This approach "cuts out the
// middleman" of the named `makeWhatever` function reference.
// 
// As explained in the above "important note," even though parens are not
// required around this function expression, they should still be used as a
// matter of convention to help clarify that the variable is being set to
// the function‘s *result* and not the function itself.

var counter = (function(){
  var i = 0;

  return {
    get: function(){
      return i;
    },
    set: function( val ){
      i = val;
    },
    increment: function() {
      return ++i;
    }
  };
}());

// `counter` is an object with properties, which in this case happen to be
// methods.

counter.get(); // 0
counter.set( 3 );
counter.increment(); // 4
counter.increment(); // 5

counter.i; // undefined (`i` is not a property of the returned object)
i; // ReferenceError: i is not defined (it only exists inside the closure)

 

原文参考:http://benalman.com/news/2010/11/immediately-invoked-function-expression/

Javascript---Immediately-Invoked Function Expression (IIFE)立即执行的函数表达式

标签:style   blog   http   io   os   ar   java   for   strong   

原文地址:http://www.cnblogs.com/IanI/p/4021956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!