Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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
jQuery中$(数组).each(…)和$.each(数组…)之间的差异_Jquery - Fatal编程技术网

jQuery中$(数组).each(…)和$.each(数组…)之间的差异

jQuery中$(数组).each(…)和$.each(数组…)之间的差异,jquery,Jquery,如果我在JavaScript变量中有一个数组需要迭代,我通常使用jQuery的。each()函数如下: var myArray = ["foo", "bar"]; $(myArray).each(function(index, value) { console.log(value); }); var myArray = ["foo", "bar"]; $.each(myArray, function(index, value) { console.log(value); });

如果我在JavaScript变量中有一个数组需要迭代,我通常使用jQuery的
。each()
函数如下:

var myArray = ["foo", "bar"];
$(myArray).each(function(index, value) {
    console.log(value);
});
var myArray = ["foo", "bar"];
$.each(myArray, function(index, value) {
    console.log(value);
});
但是我可以通过将数组作为参数传递给
.each()
函数来实现相同的效果,如下所示:

var myArray = ["foo", "bar"];
$(myArray).each(function(index, value) {
    console.log(value);
});
var myArray = ["foo", "bar"];
$.each(myArray, function(index, value) {
    console.log(value);
});


这个问题基本上也适用于任何其他jQuery函数,而不仅仅是
.each()
。这两种不同的用法在功能上有区别吗?

没有,它们的功能没有区别

主要区别在于使用
$。each()
允许您传入数组,而使用
$()。each()
要求您拥有一个构造好的jQuery对象作为上下文

在可读性上有一些细微的差别,但可以忽略不计

如果要查看,您也可以确认这一点,如
$。每个
都只是对prototype对每个的定义的调用:

// Execute a callback for every element in the matched set.
each: function( callback ) {
    return jQuery.each( this, callback );
},