Created
January 2, 2013 14:26
-
-
Save gaboesquivel/4434921 to your computer and use it in GitHub Desktop.
Javascript Reflector Object
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
// Generated by CoffeeScript 1.3.3 | |
/** | |
@author Michael Czolko <michael@czolko.cz> | |
*/ | |
var Reflector; | |
Reflector = (function() { | |
/** | |
@construct | |
@param {Object} object | |
*/ | |
function Reflector(object) { | |
this.object = object; | |
this.methods = null; | |
this.properties = null; | |
} | |
/** | |
@return array | |
*/ | |
Reflector.prototype.getProperties = function() { | |
if (this.properties === null) { | |
this.scan(); | |
} | |
return this.properties; | |
}; | |
/** | |
@return array | |
*/ | |
Reflector.prototype.getMethods = function() { | |
if (this.methods === null) { | |
this.scan(); | |
} | |
return this.methods; | |
}; | |
/** | |
@return void | |
*/ | |
Reflector.prototype.scan = function() { | |
var property; | |
this.methods = new Array; | |
this.properties = new Array; | |
for (property in this.object) { | |
if (typeof this.object[property] === 'function') { | |
this.methods.push(property); | |
} else { | |
this.properties.push(property); | |
} | |
} | |
}; | |
/** | |
@param {Function} func | |
@return array | |
*/ | |
Reflector.prototype.getParamNames = function(func) { | |
var funcStr; | |
funcStr = func.toString(); | |
return funcStr.slice(funcStr.indexOf('(') + 1, funcStr.indexOf(')')).match(/([^\s,]+)/g); | |
}; | |
/** | |
@param {Function} func | |
@return int | |
*/ | |
Reflector.prototype.getParamsCount = function(func) { | |
return this.getParamNames().length; | |
}; | |
/** | |
@return {Function} func | |
*/ | |
Reflector.prototype.getConstructor = function() { | |
return this.object.constructor; | |
}; | |
return Reflector; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment