Skip to content

Instantly share code, notes, and snippets.

@matthuhiggins
Created November 26, 2010 23:51
Show Gist options
  • Save matthuhiggins/717359 to your computer and use it in GitHub Desktop.
Save matthuhiggins/717359 to your computer and use it in GitHub Desktop.
in place prototype
Array.prototype._compact = function(iterator) {
var position = 0;
for (var index = 0, length = this.length; index < length; index++) {
if (this[index] != null)
this[position++] = this[index];
}
this.length = position;
return this;
};
Array.prototype._map = function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
for(var index = 0, length = this.length; index < length; index++) {
this[index] = iterator(this[index], index);
}
return this;
};
Array.prototype._reject = function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var position = 0;
for (var index = 0, length = this.length; index < length; index++) {
if (!iterator(this[index], index))
this[position++] = this[index];
}
this.length = position;
return this;
};
return this._reject(function(value) { return value == null; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment