Array prototype available for forEach

I have a polyfill for Array.find()which I got from MDN

The browser I'm testing now has no support, so polyfill works and works as intended. However, I have a plugin foreachfor the array that I pass to it, and the last element in it is a function find.

Why is this happening?

Also, when I check the array in Chrome DevTools, I get the following.

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>
Run codeHide result

enter image description here

Note that findin __proto__is a darker shade of pink than the other proto.

Any ideas?

+4
source share
2 answers

foreach , , . , :

Array.prototype.x = 3;
var test = [1];
for (var key in test) {
    console.log(key); 
}

... 0, x, x [], []. , foreach, :

Array.prototype.x = 3;
var test = [1];
for (var key in test) {
    if (test.hasOwnProperty(key)) {
        console.log(key); 
    }
}

, , , . :

(function () { // scope bracket; hides `find` from global scope.
    function find(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), length = list.length >>> 0, thisArg = arguments[1], value, i;
        for (i = 0; i < length; i++) {
            value = list[i];
            if (predicate.call(thisArg, value, i, list)) {
                    return value;
            }
        }
        return undefined;
    };
    if (!Array.prototype.find) {
        if (Object.defineProperty) {
            Object.defineProperty(Array.prototype, 'find', {value: find, enumerable: false});
        } else {
            Array.prototype.find = find;
        }
    }
} ());

Array.prototype.find, , Object.defineProperty, , . , , .hasOwnProperty(), foreach. ( Array.prototype.find() ECMAScript 6, Object.defineProperty() ECMAScript 5.1, Object.prototype.hasOwnProperty() - ECMAScript 3. ES5.1 2011 , , - , Opera IE, ES6 .find(), Object.defineProperty.

+4

Google Chrome , , .

Array.prototype.hi  = function() {}

console.log(Array.prototype);

, hi . find Array.prototype, , , , hi .

+2

All Articles