问题描述
您好,
我有这段代码:
Hello,
I have this code:
myfunction=function(params){
$.each(params,function(key,value){
var myfun=value["formatter"];
document.write(value["name"] + myfun());
})
}
var columns= [
{
name: "col1 ",
formatter: function () { return 'a'; }
},
{
name: "col2 ",
formatter: function () { return 'b'; }
},
{
name: "col3 ",
formatter: function () { return 'c'; }
},
]
myfunction(columns);
我的代码似乎很好,但问题是每列只对第一个元素的功能执行所有元素。什么时候应该根据元素执行功能。
我的结果:
My code seem to be good, but the problem is that to each column, only the function of the first element is executed to all elements. when should be executed the function according to the element.
my result:
col1 a
col2 a
col3 a
正确的结果应该是这个
the correct result should be this
col1 a
col2 b
col3 c
推荐答案
var columns= [
{
name: "col1 ",
formatter: function () { return 'a'; }
},
{
name: "col2 ",
formatter: function () { return 'b'; }
},
{
name: "col3 ",
formatter: function () { return 'c'; }
},
]
myfunction(columns);
我的代码似乎很好,但问题是每列只对第一个元素的功能执行所有元素。什么时候应该根据元素执行功能。
我的结果:
My code seem to be good, but the problem is that to each column, only the function of the first element is executed to all elements. when should be executed the function according to the element.
my result:
col1 a
col2 a
col3 a
正确的结果应该是这个
the correct result should be this
col1 a
col2 b
col3 c
colums[0] = function () { return 'a'; }
colums[1] = function () { return 'b'; }
colums[2] = function () { return 'c'; }
//...
// the calls would look like
value = columns[someIntegerIndex]();
我希望这只是用于研究,并且函数应该不那么简单,因为如果你真的想通过没有参数的函数返回一个字符,你最好只是一个字符数组。
这篇关于javascript中函数数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!