日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/11/30 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Javascript---Immediately-Invoked Function Expression (IIFE)立即执行的函数表达式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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/18209252090847232new 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(); // 5counter.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/

轉載于:https://www.cnblogs.com/IanI/p/4021956.html

總結

以上是生活随笔為你收集整理的Javascript---Immediately-Invoked Function Expression (IIFE)立即执行的函数表达式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。