可用数组原型我有一个Array.find()
填充工具,我从MDN中的forEach
了我目前在测试的浏览器没有为它使填充工具的运行和功能的支持,因为预期。然而,我有一个插件是foreach
通过它我最后一个元素是功能find
。
这是怎么发生的?
另外,当我在Chrome的DevTools中检查数组时,我得到以下结果。
if (!Array.find) {
$('<h5>This browser doesnt support Array.find</h5>').appendTo('body');
}
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
var someArray = [{name: 'something'}, {name: 'something else'}];
someArray.forEach(function (item, index) {
$('<h4>'+ item.name +'</h4>').appendTo('body');
});
var extended = $.extend({}, [], someArray);
$.each(extended, function (index, item) {
$('<h4>'+ item.name +'</h4>').appendTo('body');
});
if (extended.find) {
$("<div>Notice that we now have in extended the function find<div>").appendTo('body');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
注意find
在__proto__
是粉红色的比其它proto
的暗的阴影。
任何想法?
在所有浏览器中都一样吗? – 2014-10-10 14:56:29
发生了什么浏览器,你能提供代码块在哪里发生? – bigtunacan 2014-10-10 14:58:34
你在数组上执行'for..in'吗?你不应该这样做(至少不是没有'.hasOwnProperty()'调用)。 – 2014-10-10 15:06:21