Javascript:在运行时生成函数 更新
解决方案在foreach循环中有效,但在for循环中无效Javascript:在运行时生成函数 更新,javascript,lodash,Javascript,Lodash,解决方案在foreach循环中有效,但在for循环中无效 function x(number){ return number - 10; } var i = 0 var runtimefunctions = {}; var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"} for (var key in allLevels) { runtimefunc
function x(number){
return number - 10;
}
var i = 0
var runtimefunctions = {};
var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"}
for (var key in allLevels) {
runtimefunctions[i] = function() { return x(i); };
i++;
};
console.log(runtimefunctions[1]()); // -6
console.log(runtimefunctions[2]()); // -6
console.log(runtimefunctions[3]()); // -6
努力制作函数,但这是第一次创建这样的东西,所以无法理解正确的方法 我有一个功能
function x(number){
return number - 10;
}
runtimefunctions = {};
now I have a loop to run
[1,2,3].forEach(function(y){
//here I want to create a function.. which will make a function x(y) -- like this
runtimefunctions[x] = new Function("return function x_" + levelIterator + "(levelIterator){ console.log(levelIterator); x(" + y + ") }")();
});
基本上,我想做这样的函数
runtimefunctions= {
"1": x(1),
"2": x(2),
and so on
}
这是你需要的吗
function x(number){
return number - 10;
}
var runtimefunctions = {};
[1,2,3].forEach(function(y){
runtimefunctions[y] = function() { return x(y); };
});
console.log(runtimefunctions[1]()); // -9
console.log(runtimefunctions[2]()); // -8
console.log(runtimefunctions[3]()); // -7
为了满足下一个(for in)需求,您需要使用额外的函数调用来关闭索引变量:
var runtimefunctions = {}, i = 0;
var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"}
for (var key in allLevels) {
runtimefunctions[i] = function(index){ return function() { return x(index); } }(i++);
};
这是你需要的吗
function x(number){
return number - 10;
}
var runtimefunctions = {};
[1,2,3].forEach(function(y){
runtimefunctions[y] = function() { return x(y); };
});
console.log(runtimefunctions[1]()); // -9
console.log(runtimefunctions[2]()); // -8
console.log(runtimefunctions[3]()); // -7
为了满足下一个(for in)需求,您需要使用额外的函数调用来关闭索引变量:
var runtimefunctions = {}, i = 0;
var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"}
for (var key in allLevels) {
runtimefunctions[i] = function(index){ return function() { return x(index); } }(i++);
};
这要容易得多。
例如:
const createFunctionWith = (x) => {
return (param) => console.log(x, param)
}
let a = [1,2,3].map(x => createFunctionWith(x));
console.log(a[1]("bebe")); // 2, "bebe"
这要容易得多。
例如:
const createFunctionWith = (x) => {
return (param) => console.log(x, param)
}
let a = [1,2,3].map(x => createFunctionWith(x));
console.log(a[1]("bebe")); // 2, "bebe"
你可以这样做
// Found in your code
var x = (a) => {
console.log(a)
};
var runtimefunctions = {};
[1, 2, 3].forEach(function(y) {
//Create a function with a parameter named "levelIterator"
runtimefunctions[y] = Function("levelIterator", "{ console.log(levelIterator); x(" + y + ") }");
});
runtimefunctions[1]('test')
你可以这样做
// Found in your code
var x = (a) => {
console.log(a)
};
var runtimefunctions = {};
[1, 2, 3].forEach(function(y) {
//Create a function with a parameter named "levelIterator"
runtimefunctions[y] = Function("levelIterator", "{ console.log(levelIterator); x(" + y + ") }");
});
runtimefunctions[1]('test')
你是说你有一个数组,数组中的每一个值你都想调用这个函数,把这个值作为一个参数传递吗?是的,它可以有任意的数字,所以动态性太复杂了。避免在以后的求值中使用字符串,而是直接将函数分配给它们的键。关于更新:你是说你有一个数组,数组中的每个值都要调用函数,并将该值作为参数传递吗?是的,它可以有任何数字,所以动态性太复杂了。避免在以后的计算中使用字符串,而是将函数直接分配给它们的键。关于你的更新:它很有魅力,但我的错。我必须使用for循环for object而不是foreach循环..在forloop中请找到更新的问题…所有返回-6它工作起来很有魅力,但我的错。我必须使用for循环for object而不是foreach循环..在forloop中请查找更新的问题…所有返回-6