使用动态参数数组调用函数

Dav*_*der 4 coldfusion openbd cfml

如果我有

<cfset arr_arguments = ["a","b","c"]>
<cfunction name="someFunction">
 <cfargument name="someArgumentOne">
 <cfargument name="someArgumentTwo">
 <cfargument name="someArgumentThree">

</cffunction>
Run Code Online (Sandbox Code Playgroud)

有什么方法可以调用someFunction参数arr_arguments,类似于someFunction("a","b","c")? 我当然知道我可以argumentCollection用来将(键控)结构传递给函数,但我特别要求传入(无键)数组。在 JS 中,这可以很容易地完成someFunction.apply(this,arr_arguments),但在 Coldfusion 中,我找不到任何方法来做到这一点。

imt*_*tts 5

未命名的参数作为结构体传递给函数,其中数字键与函数参数中定义的参数位置相匹配。因此,您可以将数组转换为带有数字键的结构,而不是传递命名参数,然后使用参数集合传递结构:

<cfset arr_arguments = {"1"="a","2"="b","3"="c"}>
<cfset someFunction(argumentCollection=arr_arguments)>
Run Code Online (Sandbox Code Playgroud)

您可以轻松地将数组转换为带有数字键的结构,如下所示:

<cfset args = {}>
<cfloop from="1" to="#arrayLen(arr_arguments)#" index="i">
    <cfset args[i] = arr_arguments[i]>
</cfloop>
Run Code Online (Sandbox Code Playgroud)