I need to run bunch of scripts saved in text. I need to convert them to JavaScript during runtime and it may have some calls to some functions within that class to get some data, like shown below. Basically I need to expose functions of the class to those scripts being run in one of the function of the same class. It works fine as long as I do not make calls to randomNumbers() function and hard code some numbers. I'll appreciate any feedback on this. thank you,
var config = {
minX: 1,
maxX: 10,
get randomIntFromInterval() {
return (() =>
Math.floor(Math.random() * (this.maxX - this.minX + 1) + this.minX)).bind(
config
);
},
};
export const executeFns = () => {
let edit = `
let a = randomIntFromInterval();
let b = randomIntFromInterval();
if (a> b) {
console.log('a is bigger than b')
} else if (b>a) {
console.log('b is bigger than a')
}
console.log('a',a );
console.log('b',b )`;
var f4 = NamedFunction('fancyname', [], edit, config);
f4();
};
export function NamedFunction(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);
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment