摘要:最近在寫(xiě)代碼時(shí)遇到了閉包,其中閉包又和立即執(zhí)行函數(shù)有點(diǎn)關(guān)系,于是牽扯除了函數(shù)聲明以及函數(shù)表達(dá)式,我感覺(jué)中文的很多文章寫(xiě)的不太好,查閱了的指南和這篇關(guān)于的文章,覺(jué)得寫(xiě)的很好,整合一下。函數(shù)聲明和函數(shù)表達(dá)式。
最近在寫(xiě)代碼時(shí)遇到了閉包,其中閉包又和立即執(zhí)行函數(shù)(immediately invoked function expression, aka IIFE)有點(diǎn)關(guān)系,于是牽扯除了函數(shù)聲明以及函數(shù)表達(dá)式,我感覺(jué)中文的很多文章寫(xiě)的不太好,查閱了mdn的function指南和這篇關(guān)于IIFE的文章,覺(jué)得寫(xiě)的很好,整合一下。
Firstly, in JS there are two ways of defining functions: function declarations and function expressions, aka 函數(shù)聲明和函數(shù)表達(dá)式。
function declaration (function definition or function statement)
This is always followed by the name of the function (which is very important cauz function declaration requires a name, if not, it may encounter some SyntaxError, we can see from the following examples), a list of parameters and the JavaScript statements that define the function, enclosed in curly braces{}.
function expression
This can be anonymous; it doesn"t have to have a name;
var ab = function(){}; //anonymous function expression var ac = function ad() {};// function expression with a name
.
.
SyntaxError will happen in the following case when lack of a name:
var foo = function(){}; //this is a anonymous function expression we will see in the future foo(); //we can invoke the function by adding parens after foo // then some people might think of the following, since foo equals to function(){} and foo() can be used, why don"t we use function{}()? function(){}(); // However, there will be a SyntaxError: "unexpected ("
1.1 What leads to this SyntaxError?
The reason is that the parser treats it as a function declaration without a name rather than a function expression.
Now that, what about we give it a name by using
function foo(){}();// there will also be a SyntaxError: "unexpected )"
1.2 What leads to the other SyntaxError?
This is because parens placed after the stataments are view as a grouping operator which needs to contain an expression;
OK, here I finally got the idea and there must be no error! Why don"t we use this:
function foo(){}(1); //no error
1.3 In this case, however, the function is not executed either despite there is no error. Here is the reason, the parser treated this as two separate unrelated expression
function foo(){}; (1);
.
.
Is there any way to solve the problem????
Yep! The solution is IIFE(立即執(zhí)行函數(shù))?。。。。。?/p>
(function(){})(); (function(){}());
Unlike in 1.1, the parser encounters function keyword and knows that now I need to treat it like function expression rather than a function declaration! Such parens indicate that the function expression will be immediately invoked and the variable will contain the result of the function.
也就是說(shuō),只要不讓function這個(gè)關(guān)鍵詞出現(xiàn)在行首就行,所以下面的情況都可以。
~function(){}(); !function(){}();One application of IIFE -- Closure(閉包)
IIFE is used to lockin values and effectively save state.
The advantage is that since the function is invoked immediately without an identifier, a closure can be used without polluting the current scope.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/96342.html
摘要:而外層的函數(shù)不能訪問(wèn)內(nèi)層的變量或函數(shù),這樣的層層嵌套就形成了作用域鏈。閉包閉包是指有權(quán)訪問(wèn)另一個(gè)函數(shù)作用域中的變量的函數(shù),創(chuàng)建閉包的最常見(jiàn)的方式就是在一個(gè)函數(shù)內(nèi)創(chuàng)建另一個(gè)函數(shù),通過(guò)另一個(gè)函數(shù)訪問(wèn)這個(gè)函數(shù)的局部變量。 閉包是js中一個(gè)極為NB的武器,但也不折不扣的成了初學(xué)者的難點(diǎn)。因?yàn)閷W(xué)好閉包就要學(xué)好作用域,正確理解作用域鏈,然而想做到這一點(diǎn)就要深入的理解函數(shù),所以我們從函數(shù)說(shuō)起。 函數(shù)...
摘要:你需要明白的原理,我簡(jiǎn)單說(shuō)一下這是定義,定義只是讓解釋器知道其存在,但是不會(huì)運(yùn)行。這是語(yǔ)句,解釋器遇到語(yǔ)句是會(huì)運(yùn)行它的。 在SF上看到這樣一個(gè)問(wèn)題,我覺(jué)得問(wèn)得很好,所以弄成文章收集了。 沒(méi)有區(qū)別。 你需要明白 IIFE 的原理,我簡(jiǎn)單說(shuō)一下: function foo() {...} // 這是定義,Declaration;定義只是讓解釋器知道其存在,但是不會(huì)運(yùn)行。 foo(...
摘要:請(qǐng)解釋事件代理事件代理也稱為事件委托,利用了事件冒泡。同源指的是協(xié)議域名端口相同,同源策略是一種安全協(xié)議。目的同源策略保證了用戶的信息安全,瀏覽器打開(kāi)多個(gè)站點(diǎn)時(shí),互相之間不能利用獲取對(duì)方站點(diǎn)的敏感信息。 請(qǐng)解釋事件代理(event delegation) 事件代理也稱為事件委托,利用了事件冒泡。例如: item1 item2 item3 當(dāng)頁(yè)面li增多時(shí)單...
摘要:允許在塊級(jí)作用域內(nèi)聲明函數(shù)。上面代碼中,存在全局變量,但是塊級(jí)作用域內(nèi)又聲明了一個(gè)局部變量,導(dǎo)致后者綁定這個(gè)塊級(jí)作用域,所以在聲明變量前,對(duì)賦值會(huì)報(bào)錯(cuò)。 ES5的作用域 變量起作用的范圍,js中能創(chuàng)建作用域的只能是函數(shù) { let a = 1; var b = 2; } console.log(a); // a is not defined console.log(b); //...
摘要:函數(shù)表達(dá)式又名匿名函數(shù)為變量賦的值是函數(shù)定義本身。在語(yǔ)言里任何匿名函數(shù)都是屬于對(duì)象。這種情況下,就叫做回調(diào)函數(shù)。如上面代碼示例展示了文檔單擊事件時(shí)以冒泡模式傳遞給回調(diào)函數(shù)的特別適用于事件驅(qū)動(dòng)編程,因?yàn)榛卣{(diào)模式支持程序以異步方式運(yùn)行。 JavaScript設(shè)計(jì)模式的作用是提高代碼的重用性,可讀性,使代碼更容易的維護(hù)和擴(kuò)展 在javascript中,函數(shù)是一類對(duì)象,這表示他可以作為參數(shù)傳遞...
閱讀 2436·2019-08-29 13:53
閱讀 2517·2019-08-29 11:32
閱讀 3057·2019-08-28 17:51
閱讀 3803·2019-08-26 10:45
閱讀 3523·2019-08-23 17:51
閱讀 2992·2019-08-23 16:56
閱讀 3345·2019-08-23 16:25
閱讀 3099·2019-08-23 14:15