Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 关于(jquery)的说明?_Javascript_Jquery - Fatal编程技术网

Javascript 关于(jquery)的说明?

Javascript 关于(jquery)的说明?,javascript,jquery,Javascript,Jquery,下面是许多用户脚本中使用的代码: function with_jquery(f) { var script = document.createElement("script"); script.type = "text/javascript"; script.textContent = "(" + f.toString() + ")(jQuery)"; document.body.appendChild(script); }; 除了这一行,我什么都懂: script.textContent

下面是许多用户脚本中使用的代码:

function with_jquery(f) {
var script = document.createElement("script");
script.type = "text/javascript";
script.textContent = "(" + f.toString() + ")(jQuery)";
document.body.appendChild(script);
};
除了这一行,我什么都懂:

  script.textContent = "(" + f.toString() + ")(jQuery)";
我知道文本内容设置了脚本的文本内容(当然),但在
=
之后,我无法理解所有内容。这不应该是:

   script.textContent = string; 

通过使用
f.toString()
)(jQuery)
包装字符串,用户将设置要作为执行的字符串,并将jQuery对象传递给该函数

因此,我们希望
f.toString()
看起来像

function($){ [doing something...] }
这样jQuery对象将代替
$


当然,新字符串仍然需要求值

该函数使用以下代码创建脚本元素:

(content-of-f)(jQuery)
因此,如果您传递一个包含函数的字符串(例如
函数($){do something}
),并且在为
$
-参数传递
jQuery
时执行该函数。

匿名函数:

(function(){
  //Normal code goes here
})
真正有趣的是,当我们在末尾添加以下内容时会发生什么:

();
这两个小括号使前面括号中包含的所有内容 立即执行

当您编写
(jquery)(
时,如下所示:

(function(a){
  console.log(a === jquery); //Returns 'true'
})(jquery);
代码中对“jquery”的所有引用都可以重命名为“a”。检查这个