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

查看:40
本文介绍了使用 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,因为我发现它笨重",但它的优点是您可以动态循环 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天全站免登陆