Last active
August 29, 2015 14:02
-
-
Save doublejosh/16bc0017bb8d1091971c to your computer and use it in GitHub Desktop.
Ultra-light-weight basic object convenience methods class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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