It's very easy to get reflection from a JavaScript object, just as following code.
var getAllMembers = function (obj) {
var members = {};
members.properties = [];
members.methods = [];
for (var prop in obj) {
if (typeof obj[prop] != 'function')
members.properties.push(prop);
else members.methods.push(prop);
}
return members;
}
function demo_class(na, nb) {
this.a = na;
this.b = nb;
this.funca = function () {
console.log(this.a);
}
this.funcb = function () {
console.log(this.b);
}
}
var x = new demo_class(1234, 4321);
var members = getAllMembers(x);
console.log(members.properties); // [ 'a', 'b' ]
console.log(members.methods); // [ 'funca', 'funcb' ]
My question is: Is there a way to get properties and methods from a class or function, instead of from an object? For example, like this:
var members = getAllMembers(demo_class); // Only can get '[] []'
Aucun commentaire:
Enregistrer un commentaire