diff options
author | Li Jin <dragon-fly@qq.com> | 2025-06-04 16:43:26 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2025-06-04 16:43:26 +0800 |
commit | a575de6992cabe0b809e363391563841d6213d4e (patch) | |
tree | d7a711e65b114d6a2a87988c1e611b1583bc1d45 /doc/docs/zh | |
parent | 0e72454a11f65d9ac800dedb698ddfcc15933785 (diff) | |
download | yuescript-a575de6992cabe0b809e363391563841d6213d4e.tar.gz yuescript-a575de6992cabe0b809e363391563841d6213d4e.tar.bz2 yuescript-a575de6992cabe0b809e363391563841d6213d4e.zip |
Updated docs. [skip CI]
Diffstat (limited to 'doc/docs/zh')
-rwxr-xr-x | doc/docs/zh/doc/README.md | 125 |
1 files changed, 123 insertions, 2 deletions
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index b4e594c..4152591 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
@@ -585,6 +585,21 @@ merge = {...a, ...b} | |||
585 | </pre> | 585 | </pre> |
586 | </YueDisplay> | 586 | </YueDisplay> |
587 | 587 | ||
588 | ### 表反向索引 | ||
589 | |||
590 | 你可以使用 **#** 操作符来反向索引表中的元素。 | ||
591 | |||
592 | ```moonscript | ||
593 | print data.items[#] -- 获取表的最后一个元素 | ||
594 | print data.items[#-1] -- 获取表的倒数第二个元素 | ||
595 | ``` | ||
596 | <YueDisplay> | ||
597 | <pre> | ||
598 | print data.items[#] -- 获取表的最后一个元素 | ||
599 | print data.items[#-1] -- 获取表的倒数第二个元素 | ||
600 | </pre> | ||
601 | </YueDisplay> | ||
602 | |||
588 | ### 元表 | 603 | ### 元表 |
589 | 604 | ||
590 | **<>** 操作符可提供元表操作的快捷方式。 | 605 | **<>** 操作符可提供元表操作的快捷方式。 |
@@ -1285,9 +1300,55 @@ print first, second, color | |||
1285 | </pre> | 1300 | </pre> |
1286 | </YueDisplay> | 1301 | </YueDisplay> |
1287 | 1302 | ||
1288 | ### 在其它地方的解构 | 1303 | ### 范围解构 |
1304 | |||
1305 | 你可以使用展开运算符 `...` 在列表解构中来捕获一个范围的值到子列表中。这在当你想要从列表的开头和结尾提取特定元素,同时收集中间的元素时非常有用。 | ||
1306 | |||
1307 | ```moonscript | ||
1308 | orders = ["first", "second", "third", "fourth", "last"] | ||
1309 | [first, ...bulk, last] = orders | ||
1310 | print first -- 打印: first | ||
1311 | print bulk -- 打印: {"second", "third", "fourth"} | ||
1312 | print last -- 打印: last | ||
1313 | ``` | ||
1314 | <YueDisplay> | ||
1315 | <pre> | ||
1316 | orders = ["first", "second", "third", "fourth", "last"] | ||
1317 | [first, ...bulk, last] = orders | ||
1318 | print first -- 打印: first | ||
1319 | print bulk -- 打印: {"second", "third", "fourth"} | ||
1320 | print last -- 打印: last | ||
1321 | </pre> | ||
1322 | </YueDisplay> | ||
1323 | |||
1324 | 展开运算符可以用在不同的位置来捕获不同的范围,并且你可以使用 `_` 作为占位符来表示你想跳过对应范围的捕获: | ||
1325 | |||
1326 | ```moonscript | ||
1327 | -- 捕获第一个元素之后的所有元素 | ||
1328 | [first, ...rest] = orders | ||
1329 | |||
1330 | -- 捕获最后一个元素之前的所有元素 | ||
1331 | [...start, last] = orders | ||
1332 | |||
1333 | -- 跳过中间的元素,只捕获第一个和最后一个元素 | ||
1334 | [first, _..., last] = orders | ||
1335 | ``` | ||
1336 | <YueDisplay> | ||
1337 | <pre> | ||
1338 | -- 捕获第一个元素之后的所有元素 | ||
1339 | [first, ...rest] = orders | ||
1340 | |||
1341 | -- 捕获最后一个元素之前的所有元素 | ||
1342 | [...start, last] = orders | ||
1343 | |||
1344 | -- 跳过中间的元素,只捕获第一个和最后一个元素 | ||
1345 | [first, _..., last] = orders | ||
1346 | </pre> | ||
1347 | </YueDisplay> | ||
1348 | |||
1349 | ### 在其它地方的解构赋值 | ||
1289 | 1350 | ||
1290 | 解构也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环: | 1351 | 解构赋值也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环中: |
1291 | 1352 | ||
1292 | ```moonscript | 1353 | ```moonscript |
1293 | tuples = [ | 1354 | tuples = [ |
@@ -2305,6 +2366,45 @@ slice = [item for item in *items[,,2]] | |||
2305 | </pre> | 2366 | </pre> |
2306 | </YueDisplay> | 2367 | </YueDisplay> |
2307 | 2368 | ||
2369 | 最小和最大边界都可以是负数,使用负数意味着边界是从表的末尾开始计算的。 | ||
2370 | |||
2371 | ```moonscript | ||
2372 | -- 取最后4个元素 | ||
2373 | slice = [item for item in *items[-4,-1]] | ||
2374 | ``` | ||
2375 | <YueDisplay> | ||
2376 | <pre> | ||
2377 | -- 取最后4个元素 | ||
2378 | slice = [item for item in *items[-4,-1]] | ||
2379 | </pre> | ||
2380 | </YueDisplay> | ||
2381 | |||
2382 | 切片的步长也可以是负数,这意味着元素会以相反的顺序被取出。 | ||
2383 | |||
2384 | ```moonscript | ||
2385 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
2386 | ``` | ||
2387 | <YueDisplay> | ||
2388 | <pre> | ||
2389 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
2390 | </pre> | ||
2391 | </YueDisplay> | ||
2392 | |||
2393 | #### 切片表达式 | ||
2394 | |||
2395 | 切片也可以作为表达式来使用。可以用于获取一个表包含的子列表。 | ||
2396 | |||
2397 | ```moonscript | ||
2398 | -- 取第2和第4个元素作为新的列表 | ||
2399 | sub_list = items[2, 4] | ||
2400 | ``` | ||
2401 | <YueDisplay> | ||
2402 | <pre> | ||
2403 | -- 取第2和第4个元素作为新的列表 | ||
2404 | sub_list = items[2, 4] | ||
2405 | </pre> | ||
2406 | </YueDisplay> | ||
2407 | |||
2308 | ## for 循环 | 2408 | ## for 循环 |
2309 | 2409 | ||
2310 | Lua中有两种for循环形式,数字型和通用型: | 2410 | Lua中有两种for循环形式,数字型和通用型: |
@@ -2948,6 +3048,27 @@ switch tb | |||
2948 | </pre> | 3048 | </pre> |
2949 | </YueDisplay> | 3049 | </YueDisplay> |
2950 | 3050 | ||
3051 | 匹配一个列表并捕获特定范围内的元素。 | ||
3052 | |||
3053 | ```moonscript | ||
3054 | segments = ["admin", "users", "logs", "view"] | ||
3055 | switch segments | ||
3056 | when [...groups, resource, action] | ||
3057 | print "Group:", groups -- 打印: {"admin", "users"} | ||
3058 | print "Resource:", resource -- 打印: "logs" | ||
3059 | print "Action:", action -- 打印: "view" | ||
3060 | ``` | ||
3061 | <YueDisplay> | ||
3062 | <pre> | ||
3063 | segments = ["admin", "users", "logs", "view"] | ||
3064 | switch segments | ||
3065 | when [...groups, resource, action] | ||
3066 | print "Group:", groups -- 打印: {"admin", "users"} | ||
3067 | print "Resource:", resource -- 打印: "logs" | ||
3068 | print "Action:", action -- 打印: "view" | ||
3069 | </pre> | ||
3070 | </YueDisplay> | ||
3071 | |||
2951 | ## 面向对象编程 | 3072 | ## 面向对象编程 |
2952 | 3073 | ||
2953 | 在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果你想知道关于面向对象功能的实现细节,再查看Lua代码。 | 3074 | 在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果你想知道关于面向对象功能的实现细节,再查看Lua代码。 |