问题描述
我正在编写一个 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();
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)