我正在编写一个 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();