为什么 ListAppend 是非破坏性的,而 ArrayAppend 和 StructInsert 都是破坏性的?
我刚刚花了将近一个小时试图找出一个总是返回空字符串的列表的问题。我使用 ListAppend 就像使用 ArrayAppend 或 StructInsert 一样,但显然 ListAppend 的工作方式不同。 ListAppend 的工作方式与其他方式不同的原因是什么(如果有的话)?
<cfset ListAppend(list, item)>
列表 = ''
<cfset ArrayAppend(array, item)>
数组[1] = 项目
<cfset StructInsert(struct, 'key', item)>
struct.key = 项目
I just spent almost an hour trying to figure out an issue with having a list that would always return an empty string. I was using ListAppend just like one uses ArrayAppend or StructInsert, but apparently ListAppend works differently. What, if any, is the reasoning behind having ListAppend work differently from everything else?
<cfset ListAppend(list, item)>
list = ''
<cfset ArrayAppend(array, item)>
array[1] = item
<cfset StructInsert(struct, 'key', item)>
struct.key = item
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为列表只是一个大的字符串。与数组和结构不同,字符串是不可变的,这意味着它们不能更改。要“附加”新值,您需要创建一个全新的字符串。数组和结构体是可变的。所以你可以“就地”修改它们。
Possibly because a list is just a big String. Unlike arrays and structures, Strings are immutable, meaning they cannot be changed. To "append" a new value, you need to create an entirely new String. Arrays and structures are mutable. So you can modify them "in place".
ColdFusion 中的列表只是字符串,而 ColdFusion(和 Java)中的字符串是不可变的。它们无法改变。因此 ListAppend() 必须返回带有值的新字符串,而不是修改现有字符串。
Lists in ColdFusion are just Strings and strings in ColdFusion (and Java) are immutable. They cannot be changed. So ListAppend() must return the a new string with the value instead of modifying the existing string.