如何在不使用 Evaluate() 的情况下动态调用属于实例化 cfc 一部分的函数?

发布于 2024-07-06 04:22:50 字数 257 浏览 12 评论 0原文

例如,我希望能够以编程方式命中如下所示的一行代码,其中函数名称是动态分配的,而不使用 Evaluate()。 下面的代码当然不起作用,但代表了我想做的事情。

application.obj[funcName](argumentCollection=params)

我能找到动态调用函数的唯一方法是使用 cfinvoke,但据我所知,动态实例化相关的 cfc/函数,并且不能使用先前实例化的 cfc。

谢谢

For example I want to be able to programatically hit a line of code like the following where the function name is dynamically assigned without using Evaluate(). The code below of course doesn't work but represents what I would like to do.

application.obj[funcName](argumentCollection=params)

The only way I can find to call a function dynamically is by using cfinvoke, but as far as I can tell that instantiates the related cfc/function on the fly and can't use a previously instantiated cfc.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月下凄凉 2024-07-13 04:22:50

根据文档,您可以执行以下操作:

<!--- Create the component instance. --->
<cfobject component="tellTime2" name="tellTimeObj">
<!--- Invoke the methods. --->
<cfinvoke component="#tellTimeObj#" method="getLocalTime" returnvariable="localTime">
<cfinvoke component="#tellTimeObj#" method="getUTCTime" returnvariable="UTCTime">

您应该能够简单地使用 method="#myMethod#" 来调用它来动态调用特定函数。

According to the docs, you can do something like this:

<!--- Create the component instance. --->
<cfobject component="tellTime2" name="tellTimeObj">
<!--- Invoke the methods. --->
<cfinvoke component="#tellTimeObj#" method="getLocalTime" returnvariable="localTime">
<cfinvoke component="#tellTimeObj#" method="getUTCTime" returnvariable="UTCTime">

You should be able to simply call it with method="#myMethod#" to dynamically call a particular function.

倾城花音 2024-07-13 04:22:50

您可以使用 cfinvoke。 您不必指定组件。

<cfinvoke method="application.#funcName#" argumentCollection="#params#">

You can use cfinvoke. You don't have to specify a component.

<cfinvoke method="application.#funcName#" argumentCollection="#params#">
剩余の解释 2024-07-13 04:22:50

您还可以按照您想要的使用方式做一些非常相似的事情。 您可以使用您使用的语法访问对象内的方法,只是不能同时调用它。 但是,如果将其分配给临时变量,则可以调用它

<!--- get the component (has methods 'sayHi' and a method 'sayHello') --->
<cfset myObj = createObject("component", "test_object")>

<!--- set the function that we want dynamically then call it (it's a two step process) --->
<cfset func = "sayHi">
<cfset funcInstance = myObj[func]>
<cfoutput>#funcInstance("Dave")#</cfoutput>

<cfset func = "sayHello">
<cfset funcInstance = myObj[func]>
<cfoutput>#funcInstance("Dave")#</cfoutput>

You can also do something very similar, to the way you wanted to use it. You can access the method within the object using the syntax you used, you just can't call it at the same time. However, if you assign it to a temp variable, you can then call it

<!--- get the component (has methods 'sayHi' and a method 'sayHello') --->
<cfset myObj = createObject("component", "test_object")>

<!--- set the function that we want dynamically then call it (it's a two step process) --->
<cfset func = "sayHi">
<cfset funcInstance = myObj[func]>
<cfoutput>#funcInstance("Dave")#</cfoutput>

<cfset func = "sayHello">
<cfset funcInstance = myObj[func]>
<cfoutput>#funcInstance("Dave")#</cfoutput>
情话已封尘 2024-07-13 04:22:50

在 CFML 中,函数是该语言的一流成员。 这允许我们像变量一样传递它们。 在下面的示例中,我将复制名为“foobar”的函数,并在同一对象中将其重命名为“$fn”。 然后,我们可以简单地调用$fn()。

funcName = 'foobar';    
application.obj.$fn = application.obj[funcName];
application.obj.$fn(argumentCollection=arguments);

函数的上下文很重要,特别是当它引用对象的“variables”或“this”范围内的任何值时。 注意:这对于共享范围内的 CFC 实例来说不是线程安全的!

最快的方法是使用 Ben Doom 的推荐。 我只是想彻底一点。

In CFML, functions are first-class members of the language. This allows us to pass them around like a variable. In the following example I will copy the function named 'foobar' and rename it "$fn" within the same object. Then, we can simply call $fn().

funcName = 'foobar';    
application.obj.$fn = application.obj[funcName];
application.obj.$fn(argumentCollection=arguments);

The context of the function is important, especially if it references any values in the 'variables' or 'this' scope of the object. Note: this is not thread safe for CFC instances in shared scopes!

The fastest method is to use Ben Doom's recommendation. I just wanted to be thorough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文