Is this a bug in the JS standard?


Is this a bug in the JS standard?

Okay, I had to make an account just to post this.

According to docs, Array.prototype.some iterates over an array, passing each element into a predicate callback. If any invocation of the callback returns true, some exits early and returns true. If all the invocations return false, some returns false.

So theoretically, Array.prototype.some should always return the same result, regardless of the order in which the array elements exist. For example:

['1', '2', '3'].some(predicate) === ['3', '1', '2'].some(predicate) 

This seems to hold true:

['cd', 'ab'].some(function(value) { return 'abc'.startsWith(value); }); // returns true ['ab', 'cd'].some(function(value) { return 'abc'.startsWith(value); }); // also returns true 

But here's an instance where the array order changes the result:

// shouldn't this be functionally the same as 'abc'.startsWith? var predicate = String.prototype.startsWith.bind('abc'); ['ab', 'cd'].some(predicate); // returns true ['cd', 'ab'].some(predicate); // returns FALSE!! 

The same happens when you don't pre-bind but instead use the optional second 'thisArg' parameter to Array.prototype.some:

['ab', 'cd'].some(String.prototype.startsWith, 'abc'); // returns true ['cd', 'ab'].some(String.prototype.startsWith, 'abc'); // returns FALSE!! 

Try it out in your console. Completely bizarre. Anyone know what could be happening?

Submitted September 21, 2016 at 11:02PM by webdevworkinghere
via reddit http://ift.tt/2cHA1L1