Javascript Reflection

Development No Comments »

I’m not happy with the json.js script from http://json.org so I’m going to write my own.

For future reference, I’m posting the start of my object reflection class.

if(typeof Caliberweb == 'undefined') var Caliberweb = {};
(function(){
    if(typeof Caliberweb.Reflection == 'undefined') {
        Caliberweb.Reflection = {
            getMembers: function(obj,match){
                var bucket = [];
                for(var i in obj){
                    if(match(i)) bucket.push(i);
                }
                return bucket;
            },
            GetProperties: function(obj){
                return this.getMembers(obj, function(member){
                    return typeof member != 'function';
                })
            },
            GetMethods: function(obj){
                return this.getMembers(obj, function(member){
                    return typeof member == 'function';
                });
            }
        }
    }
})();