不使用“this”关键字调用库函数

pieyvz9o  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(214)

我正在编写一个javascript库来验证平面文件中存储的数据。它将测试列表(可重用的javascript代码作为文本存储在数据库中)和数据文件的记录作为数组。这里显示的当前代码试图将存储为文本的测试脚本(js)转换为javascript函数;将这些函数附加到当前对象并执行它。存储的测试脚本(文本)使用javascript库中的函数(调用它们)。只有使用调用时才能访问这些函数 this 关键字,这是合理的。然而,将有相当多的库函数,我不希望测试脚本编写者使用 this .
在当前结构中,我是否可以解决此问题?任何关于其他设计模式或架构的建议都可能有助于实现这一点。

// Library of functions exposed to caller of the library
module.exports = (function (global) {
    // 'new' an object
    var editsApp = function (edits) {
        return new editsApp.init(edits);
    };

    // prototype holds methods (to save memory space)
    editsApp.prototype = {
        randomIntFromInterval: function (minX, maxX) {
            return Math.floor(Math.random() * (maxX - minX + 1) + minX);
        },

        run: function () {
            // var test = {};
            for (let i = 0; i < this.edits.length; i++) {
                // console.log(array[i]);
                this['A' + i] = this.NamedFunction(
                    'A' + i,
                    [''],
                    this.edits[i]);

            }
            for (let i = 0; i < this.edits.length; i++) {
                // console.log(array[i]);
                this['A' + i]();
            }
        },

        NamedFunction: function (name, args, body, scope, values) {
            if (typeof args == 'string') {
                values = scope;
                scope = body;
                body = args;
                args = [];
            }

            if (!Array.isArray(scope) || !Array.isArray(values)) {
                if (typeof scope == 'object') {
                    var keys = Object.keys(scope);

                    values = keys.map(function (p) {
                        return scope[p];
                    });
                    scope = keys;
                } else {
                    values = [];
                    scope = [];
                }
            }
            return Function(
                scope,
                'function ' +
                    name +
                    '(' +
                    args.join(', ') +
                    ') {\n' +
                    body +
                    '\n}\nreturn ' +
                    name +
                    ';'
            ).apply(null, values);
        },
    };

    // the actual object is created here, allowing us to 'new' an object without calling 'new'
    editsApp.init = function (edits) {
        var self = this;

        self.edits = edits || [];
    };

    // trick borrowed from jQuery so we don't have to use the 'new' keyword
    editsApp.init.prototype = editsApp.prototype;

    // attach our editsApp to the global object, and provide a shorthand '$G' for ease our poor fingers
    global.editsApp = global.G$ = editsApp;
})(window);

// Array of test being created to run through library
export const edits = [
    `
    let c = this.randomIntFromInterval(1,10);  // WORKS 
    let d = this.randomIntFromInterval(1,10);  // WORKS
    if (c> d) {
      console.log('c is bigger than d')
    } else if (d>c) {
      console.log('d is bigger than c')
    }
        console.log('c',c );
        console.log('d',d )`,
    `
    let e = randomIntFromInterval(2,5);   // DOES NOT WORK
    let f = randomIntFromInterval(2,5);   // DOES NOT WORK
    if (e> f) {
      console.log('e is bigger than f')
    } else if (f>e) {
      console.log('f is bigger than e')
    }
        console.log('e',e );
        console.log('f',f )`,
];

// Instantiate the library, pass array of tests and execute them
var g = window.G$(edits);
g.run();

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题