将多个类似的函数附加到protoype

时间:2022-07-04 11:46:10

I want to avoid redundant code for one of my projects, which is a simple logger. This logger needs functions like *.alert, *.notice, etc. and I also decided to assign aliases to them (.alert = .al, etc.).

我想避免我的一个项目的冗余代码,这是一个简单的记录器。这个记录器需要* .alert,* .notice等函数,我还决定为它们分配别名(.alert = .al等)。

This leads to multiple similar functions, that only differ by one number, and the name.

这导致了多个类似的功能,只有一个数字和名称不同。

Module.protoype.emergency = Module.prototype.em = function () {
    this.applyLogLevel(' + (i+1) + ', arguments);
}

As an attempt to make that code-base less redundant I created this "creator-function":

为了使代码库更少冗余,我创建了这个“创建者 - 函数”:

// create logging functions
var prototypeNames = [
    ['emergency', 'emer', 'mrgc', 'em'],
    ['alert', 'aler', 'alrt', 'al'],
    ['critical', 'crit', 'crtc', 'cr'],
    ['error', 'erro', 'rror', 'er'],
    ['warning', 'warn', 'wrng', 'wa'],
    ['notice', 'noti', 'notc', 'no'],
    ['info', 'in'],
    ['debug', 'debu', 'dbug', 'de']
];

for (var i = 0; i < prototypeNames.length; i++) {
    for (var j = 0; j < prototypeNames[i].length; j++) {
        Module.prototype[prototypeNames[i][j]] = Module.prototype.Namespace.prototype[prototypeNames[i][j]] = new Function ('this.applyLogLevel(' + (i+1) + ', arguments);');
    }
}

Now my Javascript linter tells me that new Function() is a form of eval(). Is there any other/better way to solve this?

现在我的Javascript linter告诉我新的Function()是eval()的一种形式。有没有其他/更好的方法来解决这个问题?

2 个解决方案

#1


1  

I guess this is what you're looking for:

我猜这就是你要找的东西:

var prototypeNames = [
    ['emergency', 'emer', 'mrgc', 'em'],
    ['alert', 'aler', 'alrt', 'al'],
    ['critical', 'crit', 'crtc', 'cr'],
    ['error', 'erro', 'rror', 'er'],
    ['warning', 'warn', 'wrng', 'wa'],
    ['notice', 'noti', 'notc', 'no'],
    ['info', 'in'],
    ['debug', 'debu', 'dbug', 'de']
];

var Logger = {

    applyLogLevel: function(level, args) {
        document.write('log ' + level + ' ' + args[0] + '<br>');
    }

};

prototypeNames.forEach(function(names, level) {
    var fn = function() {
        this.applyLogLevel(level, arguments);
    };
    names.forEach(function(n) {
        Logger[n] = fn;
    });
});

Logger.emergency('oops')
Logger.warn('take care')
Logger.dbug('stop')

#2


2  

you can probably replace

你可以替换

new Function ('this.applyLogLevel(' + (i+1) + ', arguments);');

by

通过

(function(level) {
     return function() {
               this.applyLogLevel(level, arguments);
             }
     })(i+1);

#1


1  

I guess this is what you're looking for:

我猜这就是你要找的东西:

var prototypeNames = [
    ['emergency', 'emer', 'mrgc', 'em'],
    ['alert', 'aler', 'alrt', 'al'],
    ['critical', 'crit', 'crtc', 'cr'],
    ['error', 'erro', 'rror', 'er'],
    ['warning', 'warn', 'wrng', 'wa'],
    ['notice', 'noti', 'notc', 'no'],
    ['info', 'in'],
    ['debug', 'debu', 'dbug', 'de']
];

var Logger = {

    applyLogLevel: function(level, args) {
        document.write('log ' + level + ' ' + args[0] + '<br>');
    }

};

prototypeNames.forEach(function(names, level) {
    var fn = function() {
        this.applyLogLevel(level, arguments);
    };
    names.forEach(function(n) {
        Logger[n] = fn;
    });
});

Logger.emergency('oops')
Logger.warn('take care')
Logger.dbug('stop')

#2


2  

you can probably replace

你可以替换

new Function ('this.applyLogLevel(' + (i+1) + ', arguments);');

by

通过

(function(level) {
     return function() {
               this.applyLogLevel(level, arguments);
             }
     })(i+1);