Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Last active August 29, 2015 14:02
Show Gist options
  • Save doublejosh/16bc0017bb8d1091971c to your computer and use it in GitHub Desktop.
Save doublejosh/16bc0017bb8d1091971c to your computer and use it in GitHub Desktop.
Ultra-light-weight basic object convenience methods class.
Collection = function (obj) {
// Private vars.
var keyList = null,
length = null;
// Public functions.
return {
/**
* Get the property keys of an object.
*
* param {object} obj
* Oject to be inspected.
* return {array}
* List of object properties.
*/
keys : function() {
if (keyList === null) {
keyList = [];
for (var key in obj) keyList.push(key);
}
return keyList.sort();
},
/**
* Get the size of an object.
*
* param {object} obj
* Oject to be inspected.
* return {number}
* Size of the object.
*/
size : function () {
if (obj == null) return 0;
if (length === null) {
length = this.keys().length;
}
return length;
},
/**
* Access the object from this instance.
*
* return {object}
* Use what you started with.
*/
get : function () {
return obj;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment