javascript中函数数组的问题 [英] problem with function's array in javascript

查看:87
本文介绍了javascript中函数数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有这段代码:

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

推荐答案

.each(params, function (键,值){
var myfun = value [ formatter];
document .write(value [ name] + myfun());
})
}
.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


请参阅我对该问题的评论。做类似的事情:

Please see my comment to the question. Do something like:
colums[0] = function () { return 'a'; }
colums[1] = function () { return 'b'; }
colums[2] = function () { return 'c'; }
//...

// the calls would look like
value = columns[someIntegerIndex]();





我希望这只是用于研究,并且函数应该不那么简单,因为如果你真的想通过没有参数的函数返回一个字符,你最好只是一个字符数组。



-SA


这篇关于javascript中函数数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆