使用cfinvoke和createObject运行组件函数有什么区别? [英] What is the difference between using cfinvoke and createObject to run a component function?

查看:202
本文介绍了使用cfinvoke和createObject运行组件函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我公司的代码中,我经常看到使用的组件文件,通过初始化该组件的对象并调用对象的方法。然而,在我看来,使用cfinvoke方法更直接,特别是当只使用组件文件中的一个方法。这两种调用组件函数的方法之间有什么区别,每个方法的优缺点是什么?何时应该使用哪个?

In my company's code, I've often seen component files used by initializing an object of that component and calling the methods off the object. However, it seems to me somewhat more straightforward to use the cfinvoke method, especially when only using one method from the component file. What are the differences between these 2 methods of calling a component function and what are the pros/cons of each? When should I use which?

推荐答案

cfinvoke只能在标签中使用。

cfinvoke can only be used in tags.

createObject可用于标签& cfscript和更容易阅读IMO。

createObject can be used in both tags & cfscript and tends to be a bit slimmer / easier to read IMO.

直到最近,我避免使用cfinvoke,因为我发现它笨重,但一个pro它是你可以动态循环CFC中的方法。在createobject中你不能。

Until recently I avoided using cfinvoke because I found it "bulky" but a pro of it is you can dynamically loop over the methods within a CFC. In createobject you can't.

所以如果我有一个CFC有方法 - method1,method2,method3,method4。我可以这样循环: -

So if for example I've got a CFC which has the methods - method1, method2, method3, method4. I can loop over them like so:-

<cfloop from="1" to="4" index="element">
   <cfif structKeyExists(this,'getMethod#element#')>
<cfinvoke component="#this#" method="getLine#local.element#" returnVariable="methodValue"></cfinvoke>
<cfset arrayAppend(myArray,methodValue) />
   </cfif>

-

另一件需要注意的事情是,一些共享主机锁定在createobject。主要是因为它给下划线的Java访问。

Another thing to note is that some sharing hosts lock down on createobject. Mainly because of the access it gives to the underlining Java.

这篇关于使用cfinvoke和createObject运行组件函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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