简体   繁体   中英

NodeJs app object reflection and this is empty

I am using node.js and this , as well as, this.global is empty Anybody know why it is empty? Here is the code:

var business_object = require('./user.js');
var node_framework = require('./trace-meldaround.js');
var user = new business_object.User('Leo');
var thisObj = this;
var globalObj = this.global;
var methods = [];

for (var method in thisObj) {
    if (typeof thisObj[method] == 'function') {
        methods.push(method);
    }

    for (var method in globalObj) {
        if (typeof globalObj[method] == 'function') {
            methods.push(method);
        }
    }
}

I finally figured this out on my own and decided to post what I discovered. For Node.js, "this" is empty but module have exports. So you have to reflect on exports.

So here is my code that shows you how to setup a node.js application with modules and also how to do reflection with Node.js

file: app-separate-files.js - using cujo.js meld.js I created an Aspect Oriented Programming (AOP) Container Framework:

var business_object = require('./user.js');
var node_framework = require('./trace-meldaround.js');
var user = new business_object.User('Leo');

var trace = new node_framework.Trace('Hello Meld App');
trace.doTrace();
user.sayHello();
user.sayGoodBye();


and here is the AOP Framework Container:
trace-meldaround.js

var meld, joinPoint;
meld = require('./meld.js');
joinPoint = meld.joinpoint;
var log4js = require('./node_framework/log4js-node-master/lib/log4js'); // include log4js
log4js.configure({ // configure to use all types in different files.
    appenders: [
        {   type: 'file',
            filename: '../logs/error.log', // specify the path where u want logs folder error.log
            category: 'error',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: 'file',
            filename: '../logs/info.log', // specify the path where u want logs folder info.log
            category: 'info',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: 'file',
            filename: '../logs/debug.log', // specify the path where u want logs folder debug.log
            category: 'debug',
            maxLogSize: 20480,
            backups: 10
        }
    ]
});
var loggerinfo = log4js.getLogger('info'); // initialize the var to use.
var loggererror = log4js.getLogger('error'); // initialize the var to use.
var loggerdebug = log4js.getLogger('debug'); // initialize the var to use.
var Trace = function(AppName){
 this.appName = AppName;
 };

 Trace.prototype.doTrace = function(){

  function sayMeld(jp) {   
   console.log('meldaround function called for Application : ' + Trace.constructor.AppName);
   console.log('JoinPoint Method: ' +  jp.method);
   console.log('JoinPoint Target: ' + jp.target);
   console.log('JoinPoint Args: ' + jp.args.toString());
   console.log('JoinPoint Result: ' + jp.result);
   console.log('JoinPoint Exception: ' + jp.exception);
   loggerinfo.info('Calling Method: ' + jp.method);
   loggererror.info('This is Error Logger');
   loggerdebug.info('JoinPoint Method: ' +  jp.method);
   loggerdebug.info('JoinPoint Target: ' + jp.target);
   loggerdebug.info('JoinPoint Args: ' + jp.args.toString());
   loggerdebug.info('JoinPoint Result: ' + jp.result);
   loggerdebug.info('JoinPoint Exception: ' + jp.exception);   
  }


  var appObjects = module.parent.children;  
  var methods = [];
  var objects = [];
  for (var i=0; i<appObjects.length;i++){ 
   if (typeof appObjects[i] == 'object'){
     objects.push(appObjects[i]);

     var funcObj = appObjects[i].exports;
     if (typeof funcObj == 'object')
     {
      for (var key in funcObj){
       var k =0;
       var prototypeObj = funcObj[key].prototype;
       methods[i] = [];
       for (var proto in prototypeObj){                            
         methods[i][k] = proto;
         k++;
       }                    
      }
     }     
    }
  }

  for (var j=0; j<objects.length; j++){
   var ObjectName;
   for (var key2 in objects[j].exports){    
    ObjectName = key2;
    console.log(objects[j].exports[ObjectName].prototype);

    for (var n=0; n<methods[j].length; n++){
     if (ObjectName != 'Trace'){
     meld.around(objects[j].exports[ObjectName].prototype, methods[j][n], function(joinPoint){
      sayMeld(joinPoint);

      joinPoint.proceed();
     }); 
     }
    }
   }      
 }

  //meld.around(User.constructor.prototype, 'sayHello', function(joinPoint){
 };
 exports.Trace = Trace;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM