Skip to content

Instantly share code, notes, and snippets.

@gaboesquivel
Created January 2, 2013 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaboesquivel/4434921 to your computer and use it in GitHub Desktop.
Save gaboesquivel/4434921 to your computer and use it in GitHub Desktop.
Javascript Reflector Object
// 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