หน้าเว็บ

วันพุธที่ 16 มกราคม พ.ศ. 2556

javascript reflection


How to use?
/**
* author Redcrow
* 17/1/2012
*/
var student = new Student();
var reflections = student.getReflections();
 
console.log(reflections.getClassName());
console.log(reflections.getPrivateAttributes());
console.log(reflections.getPublicAttributes());
console.log(reflections.getAttributes());
console.log(reflections.getPrivateMethods());
console.log(reflections.getPublicMethods());
console.log(reflections.getMethods());
//

Reflection.js
/**
* author Redcrow
* 17/1/2012
*/
function Reflection(object){
    object = object || {};
    var reflect = distinguish(object);
    
    this.getClassName = function(){
        return reflect.className;
    }
    
    this.getPrivateAttributes = function(){
        return reflect.attributes.privateScope;
    }
    
    this.getPublicAttributes = function(){
        return reflect.attributes.publicScope;
    }
    
    this.getPrivateMethods = function(){
        return reflect.methods.privateScope;
    }
    
    this.getPublicMethods = function(){
        return reflect.methods.publicScope;
    } 

    this.getAttributes = function(){
        return reflect.attributes.all;
    }
    
    this.getMethods = function(){
        return reflect.methods.all;
    } 
    
    function distinguish(object){
        var reflect = {
            className : "",
     
            attributes : {
                privateScope : [],       
                publicScope : [],
                all : []
            },
     
            methods : {
                privateScope : [],       
                publicScope : [],
                all : []
            }
        }   

        reflect.className = object.publicScope.constructor.name; 
        pusher('privateScope');
        pusher('publicScope');  
        concatArray('attributes');
        concatArray('methods');
  
        function pusher(scope){
            for(i in object[scope]){
                if(typeof object[scope][i] !== 'function'){
                    reflect.attributes[scope].push(i);
                }else{
                    reflect.methods[scope].push(i);
                }
            }  
        }
  
        function concatArray(type){
            reflect[type].all = reflect[type].privateScope.concat(reflect[type].publicScope);
        }
     
        return reflect;
    }
    
}

Student.js
/**
* author Redcrow
* 17/1/2012
*/
function Student(){
    this.locale = "";
    
    var privateScope = {
        name : "",
        major : "",
        faculty : "",
        reset : function(){
     
        }
    }

    this.getName = function(){
        return privateScope.name;
    }
    
    this.setName = function(name){
        privateScope.name = name;
        return this;
    }
    
    this.getMajor = function(){
        return privateScope.major;
    }
    
    this.setMajor = function(major){
        privateScope.major = major;
        return this;
    }
    
    this.getFaculty = function(){
        return privateScope.faculty;
    }
    
    this.setFaculty = function(faculty){
        privateScope.faculty = faculty;
        return this;
    }
 
    this.getReflections = function(){
        return reflections;
    }
 
    var reflections = new Reflection({
        publicScope : this,
        privateScope : privateScope
    });
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น