Coldfusion 组件指针 [英] Coldfusion Component Pointers

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

问题描述

我正在构建的 Coldfusion 组件存在问题.好吧,我不确定这是否是一个问题或事情应该如何运作,但这似乎很奇怪.首先,我为组件的 init 函数设置参数并调用组件.但是,如果我在调用后更改其中一个参数,它也会更改组件中保存的值.这是正确的吗?

我不确定我是否解释得很好.这是一些伪代码:

init 函数将 titledata 参数存储在组件的 variables 范围内.

现在,如果我从 test 对象输出数据,我会得到 applebanana.但我希望组件保留在调用期间收到的数据.

如果我需要澄清任何事情,请告诉我.感谢您的帮助!

解决方案

我不确定这是否是一个问题或事情应该如何运作,

是的,这就是您当前代码的工作方式.这是否是您希望它的行为方式是另一个问题..

大多数复杂对象(结构、组件等)通过引用传递.这意味着您本质上只是存储指向对象的指针,而不是实际值.因此,您将始终看到对基础对象所做的任何更改.这就是 test 的输出发生变化的原因.

数组是一个例外.它们通常(但不总是)按值传递.这意味着您有一个独立的副本,它不会反映对原始对象所做的任何更改.

您的示例很有趣,因为它包含两者.对嵌套结构 (name=orange => name=banana) 的更改在组件中是可见的,因为这些结构是通过引用传递的.但父数组不是.所以你可以清除 fruit 并且它不会对组件产生影响.

示例:

测试:

<cffunction name="init" returntype="any" ...><cfargument name="title" type="string" required="true"/><cfargument name="data" type="array" required="true"/><cfset variables.title = arguments.title/><cfset variables.data = arguments.data/><返回这个/></cffunction><cffunction name="getDataArray" returntype="array"><creturn variables.data/></cffunction></cfcomponent>

<块引用>

现在,如果我从测试对象输出数据,我会得到苹果和香蕉.

没错.这是意料之中的,因为结构是通过引用传递的.如果想让组件保持独立的副本,则需要duplicate()(即深拷贝)init()里面的data元素.

 <cfset variables.data = duplicate(arguments.data)/>

I'm having an issue with a Coldfusion component that I'm building. Well, I'm not really sure if it's a problem or how things are supposed work, but it seems strange. First, I set up arguments for the init function of my component and invoke the component. But if I change one of the arguments after I invoke, it also changes the values held in the component. Is this correct?

I'm not sure if I'm explaining this very well. Here's some pseudo-code:

<cfset fruit = [] />
<cfset fruit[1] = {
    id = 1,
    name = "apple"
} />
<cfset fruit[2] = {
    id = 2,
    name = "orange"
} />

<cfset args = {
    title = "Fruit",
    data = fruit
} />

<cfinvoke component="test" method="init" returnvariable="test" argumentcollection=#args# />

<cfset fruit[2].name = "banana" />

The init function stores the title and data arguments in the component's variables scope.

Now, if I output the data from the test object, I get apple and banana. But I was expecting the component to retain the data that it received during the invoke.

Let me know if I need to clarify anything. Thanks for any help!

解决方案

I'm not really sure if it's a problem or how things are supposed work,

Yes, that is how your current code is supposed to work. Whether that is how you want it to behave is a different question ..

Most complex objects (structures, components, etectera) are passed by reference. Meaning you are essentially just storing a pointer to the object, not the actual values. So you will always see any changes made to the underlying object. That is why the output from test changes.

Arrays are an exception. They are usually (though not always) passed by value. Meaning you have an independent copy that will not reflect any changes made to the original object.

Your example is interesting because it contains both. Changes to your nested structures (name=orange => name=banana) are visible in the component because the structures are passed by reference. But the parent array is not. So you could clear fruit and it would have no effect on the component.

Example:

<cfset fruit[2].name = "bananna" />

<!--- both dumps will show "bananna" --->
<cfdump var="#fruit#" label="Fruit Before" />
<cfdump var="#test.getDataArray()#" label="Test.Data Before" />

<cfset arrayClear(fruit) />

<!--- fruit is empty and the component array is not --->
<cfdump var="#fruit#" label="Fruit After" /> 
<cfdump var="#test.getDataArray()#" label="Test.Data After" /> 

Test:

<cfcomponent>

  <cffunction name="init" returntype="any" ...>
     <cfargument name="title" type="string" required="true" />
     <cfargument name="data" type="array" required="true" />
     <cfset variables.title = arguments.title />
     <cfset variables.data = arguments.data />
     <cfreturn this />   
  </cffunction>

  <cffunction name="getDataArray" returntype="array">
     <cfreturn variables.data />
  </cffunction>

</cfcomponent>

Now, if I output the data from the test object, I get apple and banana.

Right. That is to be expected because structures are passed by reference. If you want the component to maintain a independent copy, you need to duplicate() (ie deep copy) the data element inside init().

   <cfset variables.data = duplicate(arguments.data) />

这篇关于Coldfusion 组件指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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