Rethink our indexOf implementation

This commit is contained in:
Zixaphir 2014-01-13 12:08:53 -07:00
parent 99343e8fc0
commit 63f6f5f3c2
3 changed files with 20 additions and 14 deletions

View File

@ -111,15 +111,17 @@
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Array.prototype.indexOf = function(val) {
var i;
i = this.length;
while (i--) {
Array.prototype.indexOf = function(val, i) {
var len;
i || (i = 0);
len = this.length;
while (i < len) {
if (this[i] === val) {
return i;
}
i++;
}
return i;
return -1;
};
__indexOf = [].indexOf;

View File

@ -89,15 +89,17 @@
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Array.prototype.indexOf = function(val) {
var i;
i = this.length;
while (i--) {
Array.prototype.indexOf = function(val, i) {
var len;
i || (i = 0);
len = this.length;
while (i < len) {
if (this[i] === val) {
return i;
}
i++;
}
return i;
return -1;
};
__indexOf = [].indexOf;

View File

@ -1,9 +1,11 @@
# I am bad at JavaScript and if you reuse this, so are you.
Array::indexOf = (val) ->
i = @length
while i--
Array::indexOf = (val, i) ->
i or= 0
len = @length
while i < len
return i if @[i] is val
return i
i++
return -1
# Update CoffeeScript's reference to [].indexOf
# Reserved keywords are ignored in embedded javascript.