Lua table 行为

Lua table behaviour

Table1 = {"x"}
Table2 = Table1

运行 下面的代码将改变 Table1

的值
Table2[1] = "y"
Table1[1] is now "y" instead of "x"

这背后的原因是什么?为什么其他数据类型不会发生这种情况,例如包含字符串或整数的变量?这有什么好处吗?

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

数字:

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.