Skip to content

Commit 2cd65aa

Browse files
committed
Updated 'src/metatables_and_metamethods.md'.
1 parent 98db0ee commit 2cd65aa

File tree

1 file changed

+0
-3
lines changed

1 file changed

+0
-3
lines changed

src/metatables_and_metamethods.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,6 @@ print(tab.x, tab.z) --> 10 0
439439

440440
在其中到 `setDefault` 的调用后,对 `tab` 中某个缺失字段的任何访问,都会调用其 `__index` 元方法,其就会返回零(这个元方法的 `d` 值)。
441441

442-
> **译注**:若把该元方法的 `d` 改为 `s`,就不会生效(仍然会返回 `nil`)。故可认为 Lua 将 `d` 硬编码到了其解释器中。
443-
444-
445442
那个函数 `setDefault`,创建了个新的闭包,以及给到需要某个默认值的各个表的一个新元表。在有很多表都需要默认值时,这样做的成本会很高。然而,元表将其中的默认值 `d`,连接到了他的元方法,因此我们无法为具有不同默认值的表,使用单一的元表。为了让所有表都能使用单一元表,我们可使用某个独占字段,将每个表的默认值,存储在表本身中。在不用担心名字冲突下,我们可以使用类似 `"____"` 的键,作为独占字段:
446443

447444

0 commit comments

Comments
 (0)