繁体   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