在node.js ES6中,是否可以传入一个类型然后实例化它?

时间:2022-01-29 14:40:41

In my project I have several classes that look like this:

在我的项目中,我有几个类看起来像这样:

"use strict";
var exports = module.exports = {};
var SystemsDAO = require('./dao/systems/SystemsDAO.js');
var aop = require('./dbAOPUtils.js');
var Proxy = require('harmony-proxy');
var sqlite3 = require('sqlite3').verbose();

/* Wraps a SystemServiceObject and passes in a constructed
 * DAO object as an argument to specified functions. */
exports.SystemsDAOIntercepter = function(obj) {

    let handler = {
        get(target, propKey, receiver) {
           const origMethod = target[propKey];
           return function(...args) {
            console.log('systemDAOIntercepter: BEGIN');

            // Create a reportsdao object and proxy it through an dbSQLiteConnectionIntercepter
            // (So we don't have to create it for every single method) 
            var systemdao = new SystemsDAO('blah');
            var proxSystemDAO = aop.dbSQLiteConnectionIntercepter(systemdao, sqlite3.OPEN_READONLY);

            args.push(proxSystemDAO);

            console.log('propKey: ' + target[propKey]);

            let result = null;

            result = origMethod.apply(this, args);

            console.log('systemsDAOIntercepter: END');
            return result;
           };
        }
    };

    return new Proxy(obj, handler);
};

Is it possible to pass in a type for an argument so that I only need a single DAOIntercepter class, and not one for each data access object?

是否可以为参数传入一个类型,这样我只需要一个DAOIntercepter类,而不是每个数据访问对象都有一个类?

I can see the part where I require() the SystemsDAO working for this by passing the file name, but as for instantiation of that class, I can't really see how it would be possible to do that.

我可以通过传递文件名来查看我需要()SystemsDAO为此工作的部分,但是对于该类的实例化,我无法真正看到如何做到这一点。

1 个解决方案

#1


2  

Sure - you can pass classes around like any other variable in javascript.

当然 - 你可以像在javascript中的任何其他变量一样传递类。

Note that DAOType is provided and instantiated inside the method, now.

请注意,现在在方法内部提供并实例化DAOType。

"use strict";
var exports = module.exports = {};
var aop = require('./dbAOPUtils.js');
var Proxy = require('harmony-proxy');
var sqlite3 = require('sqlite3').verbose();

/* Wraps a SystemServiceObject and passes in a constructed
 * DAO object as an argument to specified functions. */
exports.makeInterceptor = function(DAOType, obj) {

    let handler = {
        get(target, propKey, receiver) {
           const origMethod = target[propKey];
           return function(...args) {
            console.log('systemDAOIntercepter: BEGIN');

            // Create a reportsdao object and proxy it through an dbSQLiteConnectionIntercepter
            // (So we don't have to create it for every single method) 
            var dao = new DAOType('blah');
            var proxSystemDAO = aop.dbSQLiteConnectionIntercepter(dao, sqlite3.OPEN_READONLY);

            args.push(proxSystemDAO);

            console.log('propKey: ' + target[propKey]);

            let result = null;

            result = origMethod.apply(this, args);

            console.log('makeInterceptor: END');
            return result;
           };
        }
    };

    return new Proxy(obj, handler);
};

I have to admit that I would (personally) prefer to see the class instantiated externally and injected in through the arguments, however, instead of creating it inline.

我必须承认,我(个人)更愿意看到类在外部实例化并通过参数注入,但是,而不是内联创建它。

#1


2  

Sure - you can pass classes around like any other variable in javascript.

当然 - 你可以像在javascript中的任何其他变量一样传递类。

Note that DAOType is provided and instantiated inside the method, now.

请注意,现在在方法内部提供并实例化DAOType。

"use strict";
var exports = module.exports = {};
var aop = require('./dbAOPUtils.js');
var Proxy = require('harmony-proxy');
var sqlite3 = require('sqlite3').verbose();

/* Wraps a SystemServiceObject and passes in a constructed
 * DAO object as an argument to specified functions. */
exports.makeInterceptor = function(DAOType, obj) {

    let handler = {
        get(target, propKey, receiver) {
           const origMethod = target[propKey];
           return function(...args) {
            console.log('systemDAOIntercepter: BEGIN');

            // Create a reportsdao object and proxy it through an dbSQLiteConnectionIntercepter
            // (So we don't have to create it for every single method) 
            var dao = new DAOType('blah');
            var proxSystemDAO = aop.dbSQLiteConnectionIntercepter(dao, sqlite3.OPEN_READONLY);

            args.push(proxSystemDAO);

            console.log('propKey: ' + target[propKey]);

            let result = null;

            result = origMethod.apply(this, args);

            console.log('makeInterceptor: END');
            return result;
           };
        }
    };

    return new Proxy(obj, handler);
};

I have to admit that I would (personally) prefer to see the class instantiated externally and injected in through the arguments, however, instead of creating it inline.

我必须承认,我(个人)更愿意看到类在外部实例化并通过参数注入,但是,而不是内联创建它。