jQuery.each实现从本地Array.forEach不同 [英] jQuery.each implementation differs from native Array.forEach

查看:151
本文介绍了jQuery.each实现从本地Array.forEach不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人为什么是优秀的,否则 jQuery.each 函数是从(现在的)原生 Array.forEach ? F.ex:

Does anyone why is the otherwise excellent jQuery.each function is designed differently from the (now) native Array.forEach? F.ex:

var arr = ['abc','def'];
arr.forEach(function(entry, index) {
    console.log(entry); // abc / def
});

这使得绝对意义。但jQuery的选择了把首页作为第一个参数:

This makes absolute sense. But jQuery chose to put the index as first argument:

$.each(arr, function(index, entry) {
   console.log(entry);
});

有谁知道这个设计决定背后的原因?我一直使用 $。每个广泛,但它总是窃听我该指数的第一个参数,因为它很少使用。我知道jQuery的实现通过直接引用这个,但如果你这样做是非常令人困惑:

Does anyone know the reasoning behind this design decision? I have always used $.each extensively, but it always bugged me that the index was the first argument as it is rarely used. I know jQuery implemented a direct reference through this but it’s very confusing if you do:

​var arr = ['abc','def'];
$.each(arr, function() {
    console.log(this === 'abc'); // false both times, since this is a String constructor
});​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

不在于它困扰我这么多,我preFER使用本地polyfills为最常见的新数组功能,但我一直好奇的设计决策。也许这是在旧时代之前浏览器中实现本土制作的forEach 和传统支持prevented他们改变它,还是...?

Not that it bothers me so much, I prefer to use native polyfills for the most common new array functions, but I have always been curious about the design decision. Maybe it was made in older times before browsers implemented native forEach and legacy support prevented them from changing it, or...?

或许,它的目的是通过这种方式,因为是可以对本地对象使用过,比那有道理把键值之前在回调...?

Or maybe, it is designed this way because is can be used on native objects too, than then it "makes sense" to put the key before value in the callback...?

旁注:我知道underscore.js(也许其他库)做它周围的其他方式(更类似于本地函数)

Sidenote: I know underscore.js (and maybe other libraries) does it the other way around (more similar to the native function).

推荐答案

好吧,我想我们不得不问Resig的先生亲自为这一解释。事实是,的ECMAScript 262版5 的不是很wides $ P $垫jQuery的设计和开发时间,所以这绝对用武之地。而且由于它的设计像这样,他们不希望改变它,打破现有的所有code。

Well, I guess we'd have to ask Mr. Resig himself for an explanation on this. Fact is, that ECMAscript 262 edition 5 wasn't very widespread the time jQuery was designed and developed, so this definitely comes into play. And since it was designed like so, they didn't want to change it later and break all existing code.

在事实上,它要具有较高的优先级来访问一个元素,比它的指数更可能是循环的阵列时的。所以,对我来说没有任何合理的解释,为什么你会通过在首页的先入回调。

In fact, its much more likely that you want to access an element with a higher priority, than the index of it when looping an Array. So, to me there is no reasonable explanation why you would pass in the index first into the callbacks.

放心,如果jQuery的今天发明的,他们将遵循本机实现的行为。

Be assured, if jQuery was invented today, they would follow the native implementation behavior.

在另一方面,如果你的错误太多,你可以简单地创建一个快捷方式,并使用本地 Array.prototype.forEach 来迭代您的jQuery包裹集:

On the other hand, if it bugs you too much you can simply create a shortcut and use the native Array.prototype.forEach to iterate your jQuery wrapped sets:

var forEach = Function.prototype.call.bind( Array.prototype.forEach );

forEach( $('div'), function( node ) {
    console.log( node );
});

..和标准的阵列的,只是去与他们本地的原型。

..and for standard Arrays, just go with their native prototype.

这篇关于jQuery.each实现从本地Array.forEach不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆