From aff85ce82dcdf5605cba05b77207845ebcd19675 Mon Sep 17 00:00:00 2001 From: SgtWilko Date: Fri, 16 Mar 2018 13:01:10 +0000 Subject: [PATCH] use defineProperty to declare function. This converts the old declaration which confuses code such as `for(x in y)` which iterates over the array object to being declared using defineProperty which doesn't cause the issue. Fixes jsPolyfill/Array.prototype.find#2 --- find.js | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/find.js b/find.js index cb1372b..a96a733 100644 --- a/find.js +++ b/find.js @@ -1,19 +1,25 @@ 'use strict'; -Array.prototype.find = Array.prototype.find || function(callback) { - if (this === null) { - throw new TypeError('Array.prototype.find called on null or undefined'); - } else if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - var list = Object(this); - // Makes sures is always has an positive integer as length. - var length = list.length >>> 0; - var thisArg = arguments[1]; - for (var i = 0; i < length; i++) { - var element = list[i]; - if ( callback.call(thisArg, element, i, list) ) { - return element; +//Array.prototype.find = Array.prototype.find || function(callback) { +if (!Array.prototype.find) { + //Define as Property otherwise for(x in y) will loop over the function. + Object.defineProperty(Array.prototype, 'find', { + value: function(callback){ + if (this === null) { + throw new TypeError('Array.prototype.find called on null or undefined'); + } else if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + var list = Object(this); + // Makes sures is always has an positive integer as length. + var length = list.length >>> 0; + var thisArg = arguments[1]; + for (var i = 0; i < length; i++) { + var element = list[i]; + if ( callback.call(thisArg, element, i, list) ) { + return element; + } + } } - } -}; + }); +}