Lua表行为

时间:2018-05-16 07:51:58

标签: lua lua-table

string[start:end]

运行以下代码会更改Table1 = {"x"} Table2 = Table1

的值
Table1

这背后的原因是什么?为什么这不会发生在包含字符串或整数的变量等其他数据类型中?这有什么好处?

1 个答案:

答案 0 :(得分:1)

简单说明:
变量不包含对象 变量包含对象的引用。

代表数字:

Number1 = 42
-- Variable refers to an object (a number 42)
Number2 = Number1 
-- Both variables refer to the same object (a number 42)
Number2 = Number2 + 1
-- You created new object (a number 43) and made variable Number2 refer to this new object.
表格

Table1 = {"x"}
-- Variable refers to an object (an array containing string "x")
Table2 = Table1 
-- Both variables refer to the same object (an array containing string "x")
Table2[1] = "y"
-- You modified existing object.  You didn't create new one.