Skip to content

Instantly share code, notes, and snippets.

@optikalefx
Created November 28, 2012 18:05
Show Gist options
  • Save optikalefx/4162926 to your computer and use it in GitHub Desktop.
Save optikalefx/4162926 to your computer and use it in GitHub Desktop.
window.Root.Collection = _array.define({
has: function(value) {
if ( this.indexOf(value) !== -1 ) {
return true;
} else {
return false;
}
},
add: function(value) {
if ( !this.has(value) ) {
this.push(value);
return true;
} else {
return false;
}
},
remove: function(value) {
var index = this.indexOf(value);
if ( index !== -1 ) {
this.splice(index, 1);
}
return this;
},
clear: function() {
this.splice(0, this.length);
},
index: function() {
return this.indexOf(this);
},
random: function() {
return this[Math.ceil(Math.random() * this.length - 1)];
},
get: function(item){
return this[item];
},
toObject : function() {
var obj = JSON.parse(JSON.stringify(this));
delete obj.length;
return obj;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment