簡體   English   中英

理解javascript函數聲明

[英]Understand javascript function declaration

我知道 $ 是 jQuery 的別名。 下面的代碼將一個匿名函數傳遞給 $ 函數,該函數可以接受一個回調函數。

$(document).ready(function() {
    // ...
});

完美的。 它簡單明了。 但是在其中一個網站中,我看到以下兩個文件,但無法理解它在做什么。

文件 1

(function($, window, document, ns) {
    "use strict";
    ...
    ...
})
($, window, document, Granite.author);

檔案 2

(function($,document) {
    "use strict";
    ...
    ...
})(Granite.$, document);

任何人都可以幫助我了解我們傳遞的 $、窗口、文檔、ns 等內容嗎?

這不一定是 jQuery 概念。 它正在創建一個函數並立即調用它。

像這樣的東西:

 (function (argument) { console.log(argument) })("This is being passed to the function")

另一種使用箭頭函數的方法是

 ((argument) => { console.log(argument) })("This is being passed to the arrow function")

如果你想要一個調用他自己的函數,你可以在 (function hello(){ console.log("hello") })() 之間定義它

這會自動調用該函數並記錄“hello”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM