aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/zh
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs/zh')
-rwxr-xr-xdoc/docs/zh/doc/README.md164
1 files changed, 85 insertions, 79 deletions
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index 355e468..f34fe58 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -31,16 +31,19 @@ inventory =
31 31
32-- 管道操作符 32-- 管道操作符
33[1, 2, 3] 33[1, 2, 3]
34 |> map (x)-> x * 2 34 |> map (x) -> x * 2
35 |> filter (x)-> x > 4 35 |> filter (x) -> x > 4
36 |> reduce 0, (a, b)-> a + b 36 |> reduce 0, (a, b) -> a + b
37 |> print 37 |> print
38 38
39-- 元表操作 39-- 元表操作
40apple = 40apple =
41 size: 15 41 size: 15
42 <index>: {color: 0x00ffff} 42 <index>:
43p apple.color, apple.<index> if apple.<>? 43 color: 0x00ffff
44
45with apple
46 p .size, .color, .<index> if .<>?
44 47
45-- 类似js的导出语法 48-- 类似js的导出语法
46export 🌛 = "月之脚本" 49export 🌛 = "月之脚本"
@@ -63,16 +66,19 @@ inventory =
63 66
64-- 管道操作符 67-- 管道操作符
65[1, 2, 3] 68[1, 2, 3]
66 |> map (x)-> x * 2 69 |> map (x) -> x * 2
67 |> filter (x)-> x > 4 70 |> filter (x) -> x > 4
68 |> reduce 0, (a, b)-> a + b 71 |> reduce 0, (a, b) -> a + b
69 |> print 72 |> print
70 73
71-- 元表操作 74-- 元表操作
72apple = 75apple =
73 size: 15 76 size: 15
74 &lt;index&gt;: {color: 0x00ffff} 77 &lt;index&gt;:
75p apple.color, apple.&lt;index&gt; if apple.&lt;&gt;? 78 color: 0x00ffff
79
80with apple
81 p .size, .color, .&lt;index&gt; if .&lt;&gt;?
76 82
77-- 类似js的导出语法 83-- 类似js的导出语法
78export 🌛 = "月之脚本" 84export 🌛 = "月之脚本"
@@ -209,14 +215,14 @@ area = $PI2 * 5
209macro HELLO = -> "'你好 世界'" 215macro HELLO = -> "'你好 世界'"
210print $HELLO 216print $HELLO
211 217
212macro config = (debugging)-> 218macro config = (debugging) ->
213 global debugMode = debugging == "true" 219 global debugMode = debugging == "true"
214 "" 220 ""
215 221
216macro asserts = (cond)-> 222macro asserts = (cond) ->
217 debugMode and "assert #{cond}" or "" 223 debugMode and "assert #{cond}" or ""
218 224
219macro assert = (cond)-> 225macro assert = (cond) ->
220 debugMode and "assert #{cond}" or "#{cond}" 226 debugMode and "assert #{cond}" or "#{cond}"
221 227
222$config true 228$config true
@@ -226,7 +232,7 @@ $config false
226value = $assert item 232value = $assert item
227 233
228-- 宏函数参数传递的表达式会被转换为字符串 234-- 宏函数参数传递的表达式会被转换为字符串
229macro and = (...)-> "#{ table.concat {...}, ' and ' }" 235macro and = (...) -> "#{ table.concat {...}, ' and ' }"
230if $and f1!, f2!, f3! 236if $and f1!, f2!, f3!
231 print "OK" 237 print "OK"
232``` 238```
@@ -238,14 +244,14 @@ area = $PI2 * 5
238macro HELLO = -> "'你好 世界'" 244macro HELLO = -> "'你好 世界'"
239print $HELLO 245print $HELLO
240 246
241macro config = (debugging)-> 247macro config = (debugging) ->
242 global debugMode = debugging == "true" 248 global debugMode = debugging == "true"
243 "" 249 ""
244 250
245macro asserts = (cond)-> 251macro asserts = (cond) ->
246 debugMode and "assert #{cond}" or "" 252 debugMode and "assert #{cond}" or ""
247 253
248macro assert = (cond)-> 254macro assert = (cond) ->
249 debugMode and "assert #{cond}" or "#{cond}" 255 debugMode and "assert #{cond}" or "#{cond}"
250 256
251$config true 257$config true
@@ -255,7 +261,7 @@ $config false
255value = $assert item 261value = $assert item
256 262
257-- 宏函数参数传递的表达式会被转换为字符串 263-- 宏函数参数传递的表达式会被转换为字符串
258macro and = (...)-> "#{ table.concat {...}, ' and ' }" 264macro and = (...) -> "#{ table.concat {...}, ' and ' }"
259if $and f1!, f2!, f3! 265if $and f1!, f2!, f3!
260 print "OK" 266 print "OK"
261</pre> 267</pre>
@@ -265,20 +271,20 @@ if $and f1!, f2!, f3!
265 271
266宏函数可以返回一个包含月之脚本代码的字符串,或是一个包含Lua代码字符串的配置表。 272宏函数可以返回一个包含月之脚本代码的字符串,或是一个包含Lua代码字符串的配置表。
267```moonscript 273```moonscript
268macro yueFunc = (var)-> "local #{var} = ->" 274macro yueFunc = (var) -> "local #{var} = ->"
269$yueFunc funcA 275$yueFunc funcA
270funcA = -> "访问月之脚本定义的变量" 276funcA = -> "无法访问宏生之脚本定义的变量"
271 277
272-- 让月之脚本知道你在Lua代码中声明的局部变量 278-- 让月之脚本知道你在Lua代码中声明的局部变量
273macro luaFunc = (var)-> { 279macro luaFunc = (var) -> {
274 code: "local function #{var}() end" 280 code: "local function #{var}() end"
275 type: "lua" 281 type: "lua"
276 locals: {var} 282 locals: {var}
277} 283}
278$luaFunc funcB 284$luaFunc funcB
279funcB = -> "访问Lua代码里定义的变量" 285funcB = -> "访问宏生成Lua代码里定义的变量"
280 286
281macro lua = (code)-> { 287macro lua = (code) -> {
282 :code 288 :code
283 type: "lua" 289 type: "lua"
284} 290}
@@ -293,20 +299,20 @@ end
293``` 299```
294<YueDisplay> 300<YueDisplay>
295<pre> 301<pre>
296macro yueFunc = (var)-> "local #{var} = ->" 302macro yueFunc = (var) -> "local #{var} = ->"
297$yueFunc funcA 303$yueFunc funcA
298funcA = -> "访问月之脚本定义的变量" 304funcA = -> "无法访问宏生之脚本定义的变量"
299 305
300-- 让月之脚本知道你在Lua代码中声明的局部变量 306-- 让月之脚本知道你在Lua代码中声明的局部变量
301macro luaFunc = (var)-> { 307macro luaFunc = (var) -> {
302 code: "local function #{var}() end" 308 code: "local function #{var}() end"
303 type: "lua" 309 type: "lua"
304 locals: {var} 310 locals: {var}
305} 311}
306$luaFunc funcB 312$luaFunc funcB
307funcB = -> "访问Lua代码里定义的变量" 313funcB = -> "访问宏生成Lua代码里定义的变量"
308 314
309macro lua = (code)-> { 315macro lua = (code) -> {
310 :code 316 :code
311 type: "lua" 317 type: "lua"
312} 318}
@@ -326,9 +332,9 @@ end
326宏函数可以从一个模块中导出,并在另一个模块中导入。您必须将导出的宏函数放在一个单独的文件中使用,而且只有宏定义、宏导入和宏展开可以放入这个宏导出模块中。 332宏函数可以从一个模块中导出,并在另一个模块中导入。您必须将导出的宏函数放在一个单独的文件中使用,而且只有宏定义、宏导入和宏展开可以放入这个宏导出模块中。
327```moonscript 333```moonscript
328-- 文件: utils.yue 334-- 文件: utils.yue
329export macro map = (items, action)-> "[#{action} for _ in *#{items}]" 335export macro map = (items, action) -> "[#{action} for _ in *#{items}]"
330export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" 336export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]"
331export macro foreach = (items, action)-> "for _ in *#{items} 337export macro foreach = (items, action) -> "for _ in *#{items}
332 #{action}" 338 #{action}"
333 339
334-- 文件 main.yue 340-- 文件 main.yue
@@ -341,9 +347,9 @@ import "utils" as {
341<YueDisplay> 347<YueDisplay>
342<pre> 348<pre>
343-- 文件: utils.yue 349-- 文件: utils.yue
344export macro map = (items, action)-> "[#{action} for _ in *#{items}]" 350export macro map = (items, action) -> "[#{action} for _ in *#{items}]"
345export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" 351export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]"
346export macro foreach = (items, action)-> "for _ in *#{items} 352export macro foreach = (items, action) -> "for _ in *#{items}
347 #{action}" 353 #{action}"
348-- 文件 main.yue 354-- 文件 main.yue
349-- 在浏览器中不支持import函数,请在真实环境中尝试 355-- 在浏览器中不支持import函数,请在真实环境中尝试
@@ -453,7 +459,7 @@ print 1 <= a <= 10
453可以注意一下链式比较表达式的求值行为: 459可以注意一下链式比较表达式的求值行为:
454 460
455```moonscript 461```moonscript
456v = (x)-> 462v = (x) ->
457 print x 463 print x
458 x 464 x
459 465
@@ -476,7 +482,7 @@ print v(1) > v(2) <= v(3)
476``` 482```
477<YueDisplay> 483<YueDisplay>
478<pre> 484<pre>
479v = (x)-> 485v = (x) ->
480 print x 486 print x
481 x 487 x
482 488
@@ -564,7 +570,7 @@ merge = {...a, ...b}
564 570
565```moonscript 571```moonscript
566mt = {} 572mt = {}
567add = (right)=> <>: mt, value: @value + right.value 573add = (right) => <>: mt, value: @value + right.value
568mt.__add = add 574mt.__add = add
569 575
570a = <>: mt, value: 1 576a = <>: mt, value: 1
@@ -580,7 +586,7 @@ close _ = <close>: -> print "超出范围"
580<YueDisplay> 586<YueDisplay>
581<pre> 587<pre>
582mt = {} 588mt = {}
583add = (right)=> &lt;&gt;: mt, value: @value + right.value 589add = (right) => &lt;&gt;: mt, value: @value + right.value
584mt.__add = add 590mt.__add = add
585 591
586a = &lt;&gt;: mt, value: 1 592a = &lt;&gt;: mt, value: 1
@@ -642,7 +648,7 @@ func?!
642print abc?["你好 世界"]?.xyz 648print abc?["你好 世界"]?.xyz
643 649
644x = tab?.value 650x = tab?.value
645len = utf8?.len or string?.len or (o)-> #o 651len = utf8?.len or string?.len or (o) -> #o
646 652
647if print and x? 653if print and x?
648 print x 654 print x
@@ -657,7 +663,7 @@ func?!
657print abc?["你好 世界"]?.xyz 663print abc?["你好 世界"]?.xyz
658 664
659x = tab?.value 665x = tab?.value
660len = utf8?.len or string?.len or (o)-> #o 666len = utf8?.len or string?.len or (o) -> #o
661 667
662if print and x? 668if print and x?
663 print x 669 print x
@@ -1358,18 +1364,18 @@ print ok, count, first
1358```moonscript 1364```moonscript
1359Rx.Observable 1365Rx.Observable
1360 .fromRange 1, 8 1366 .fromRange 1, 8
1361 \filter (x)-> x % 2 == 0 1367 \filter (x) -> x % 2 == 0
1362 \concat Rx.Observable.of 'who do we appreciate' 1368 \concat Rx.Observable.of 'who do we appreciate'
1363 \map (value)-> value .. '!' 1369 \map (value) -> value .. '!'
1364 \subscribe print 1370 \subscribe print
1365``` 1371```
1366<YueDisplay> 1372<YueDisplay>
1367<pre> 1373<pre>
1368Rx.Observable 1374Rx.Observable
1369 .fromRange 1, 8 1375 .fromRange 1, 8
1370 \filter (x)-> x % 2 == 0 1376 \filter (x) -> x % 2 == 0
1371 \concat Rx.Observable.of 'who do we appreciate' 1377 \concat Rx.Observable.of 'who do we appreciate'
1372 \map (value)-> value .. '!' 1378 \map (value) -> value .. '!'
1373 \subscribe print 1379 \subscribe print
1374</pre> 1380</pre>
1375</YueDisplay> 1381</YueDisplay>
@@ -1583,11 +1589,11 @@ func_b()
1583带有参数的函数可以通过在箭头前加上括号中的参数名列表来进行创建: 1589带有参数的函数可以通过在箭头前加上括号中的参数名列表来进行创建:
1584 1590
1585```moonscript 1591```moonscript
1586sum = (x, y)-> print "数字的和", x + y 1592sum = (x, y) -> print "数字的和", x + y
1587``` 1593```
1588<YueDisplay> 1594<YueDisplay>
1589<pre> 1595<pre>
1590sum = (x, y)-> print "数字的和", x + y 1596sum = (x, y) -> print "数字的和", x + y
1591</pre> 1597</pre>
1592</YueDisplay> 1598</YueDisplay>
1593 1599
@@ -1624,7 +1630,7 @@ print "x:", sum(10, 20), "y:", sum(30, 40)
1624函数会将函数体中的最后一个语句强制转换为返回语句,这被称作隐式返回: 1630函数会将函数体中的最后一个语句强制转换为返回语句,这被称作隐式返回:
1625 1631
1626```moonscript 1632```moonscript
1627sum = (x, y)-> x + y 1633sum = (x, y) -> x + y
1628print "数字的和是", sum 10, 20 1634print "数字的和是", sum 10, 20
1629``` 1635```
1630<YueDisplay> 1636<YueDisplay>
@@ -1637,23 +1643,23 @@ print "数字的和是", sum 10, 20
1637如果您需要做显式返回,可以使用return关键字: 1643如果您需要做显式返回,可以使用return关键字:
1638 1644
1639```moonscript 1645```moonscript
1640sum = (x, y)-> return x + y 1646sum = (x, y) -> return x + y
1641``` 1647```
1642<YueDisplay> 1648<YueDisplay>
1643<pre> 1649<pre>
1644sum = (x, y)-> return x + y 1650sum = (x, y) -> return x + y
1645</pre> 1651</pre>
1646</YueDisplay> 1652</YueDisplay>
1647 1653
1648就像在Lua中一样,函数可以返回多个值。最后一个语句必须是由逗号分隔的值列表: 1654就像在Lua中一样,函数可以返回多个值。最后一个语句必须是由逗号分隔的值列表:
1649 1655
1650```moonscript 1656```moonscript
1651mystery = (x, y)-> x + y, x - y 1657mystery = (x, y) -> x + y, x - y
1652a, b = mystery 10, 20 1658a, b = mystery 10, 20
1653``` 1659```
1654<YueDisplay> 1660<YueDisplay>
1655<pre> 1661<pre>
1656mystery = (x, y)-> x + y, x - y 1662mystery = (x, y) -> x + y, x - y
1657a, b = mystery 10, 20 1663a, b = mystery 10, 20
1658</pre> 1664</pre>
1659</YueDisplay> 1665</YueDisplay>
@@ -1663,11 +1669,11 @@ a, b = mystery 10, 20
1663因为在Lua中调用方法时,经常习惯将对象作为第一个参数传入,所以月之脚本提供了一种特殊的语法来创建自动包含self参数的函数。 1669因为在Lua中调用方法时,经常习惯将对象作为第一个参数传入,所以月之脚本提供了一种特殊的语法来创建自动包含self参数的函数。
1664 1670
1665```moonscript 1671```moonscript
1666func = (num)=> @value + num 1672func = (num) => @value + num
1667``` 1673```
1668<YueDisplay> 1674<YueDisplay>
1669<pre> 1675<pre>
1670func = (num)=> @value + num 1676func = (num) => @value + num
1671</pre> 1677</pre>
1672</YueDisplay> 1678</YueDisplay>
1673 1679
@@ -1676,13 +1682,13 @@ func = (num)=> @value + num
1676可以为函数的参数提供默认值。如果参数的值为nil,则确定该参数为空。任何具有默认值的nil参数在函数体运行之前都会被替换。 1682可以为函数的参数提供默认值。如果参数的值为nil,则确定该参数为空。任何具有默认值的nil参数在函数体运行之前都会被替换。
1677 1683
1678```moonscript 1684```moonscript
1679my_function = (name = "某物", height = 100)-> 1685my_function = (name = "某物", height = 100) ->
1680 print "你好,我是", name 1686 print "你好,我是", name
1681 print "我的高度是", height 1687 print "我的高度是", height
1682``` 1688```
1683<YueDisplay> 1689<YueDisplay>
1684<pre> 1690<pre>
1685my_function = (name = "某物", height = 100)-> 1691my_function = (name = "某物", height = 100) ->
1686 print "你好,我是", name 1692 print "你好,我是", name
1687 print "我的高度是", height 1693 print "我的高度是", height
1688</pre> 1694</pre>
@@ -1691,12 +1697,12 @@ my_function = (name = "某物", height = 100)->
1691函数参数的默认值表达式在函数体中会按参数声明的顺序进行计算。因此,在默认值的表达式中可以访问先前声明的参数。 1697函数参数的默认值表达式在函数体中会按参数声明的顺序进行计算。因此,在默认值的表达式中可以访问先前声明的参数。
1692 1698
1693```moonscript 1699```moonscript
1694some_args = (x = 100, y = x + 1000)-> 1700some_args = (x = 100, y = x + 1000) ->
1695 print x + y 1701 print x + y
1696``` 1702```
1697<YueDisplay> 1703<YueDisplay>
1698<pre> 1704<pre>
1699some_args = (x = 100, y = x + 1000)-> 1705some_args = (x = 100, y = x + 1000) ->
1700 print x + y 1706 print x + y
1701</pre> 1707</pre>
1702</YueDisplay> 1708</YueDisplay>
@@ -2747,7 +2753,7 @@ class Inventory
2747 new: => 2753 new: =>
2748 @items = {} 2754 @items = {}
2749 2755
2750 add_item: (name)=> 2756 add_item: (name) =>
2751 if @items[name] 2757 if @items[name]
2752 @items[name] += 1 2758 @items[name] += 1
2753 else 2759 else
@@ -2759,7 +2765,7 @@ class Inventory
2759 new: => 2765 new: =>
2760 @items = {} 2766 @items = {}
2761 2767
2762 add_item: (name)=> 2768 add_item: (name) =>
2763 if @items[name] 2769 if @items[name]
2764 @items[name] += 1 2770 @items[name] += 1
2765 else 2771 else
@@ -2798,7 +2804,7 @@ inv\add_item "pants"
2798```moonscript 2804```moonscript
2799class Person 2805class Person
2800 clothes: [] 2806 clothes: []
2801 give_item: (name)=> 2807 give_item: (name) =>
2802 table.insert @clothes, name 2808 table.insert @clothes, name
2803 2809
2804a = Person! 2810a = Person!
@@ -2814,7 +2820,7 @@ print item for item in *a.clothes
2814<pre> 2820<pre>
2815class Person 2821class Person
2816 clothes: [] 2822 clothes: []
2817 give_item: (name)=> 2823 give_item: (name) =>
2818 table.insert @clothes, name 2824 table.insert @clothes, name
2819 2825
2820a = Person! 2826a = Person!
@@ -2850,7 +2856,7 @@ class Person
2850```moonscript 2856```moonscript
2851class BackPack extends Inventory 2857class BackPack extends Inventory
2852 size: 10 2858 size: 10
2853 add_item: (name)=> 2859 add_item: (name) =>
2854 if #@items > size then error "背包已满" 2860 if #@items > size then error "背包已满"
2855 super name 2861 super name
2856``` 2862```
@@ -2858,7 +2864,7 @@ class BackPack extends Inventory
2858<pre> 2864<pre>
2859class BackPack extends Inventory 2865class BackPack extends Inventory
2860 size: 10 2866 size: 10
2861 add_item: (name)=> 2867 add_item: (name) =>
2862 if #@items > size then error "背包已满" 2868 if #@items > size then error "背包已满"
2863 super name 2869 super name
2864</pre> 2870</pre>
@@ -2873,7 +2879,7 @@ class BackPack extends Inventory
2873 2879
2874```moonscript 2880```moonscript
2875class Shelf 2881class Shelf
2876 @__inherited: (child)=> 2882 @__inherited: (child) =>
2877 print @__name, "被", child.__name, "继承" 2883 print @__name, "被", child.__name, "继承"
2878 2884
2879-- 将打印: Shelf 被 Cupboard 继承 2885-- 将打印: Shelf 被 Cupboard 继承
@@ -2882,7 +2888,7 @@ class Cupboard extends Shelf
2882<YueDisplay> 2888<YueDisplay>
2883<pre> 2889<pre>
2884class Shelf 2890class Shelf
2885 @__inherited: (child)=> 2891 @__inherited: (child) =>
2886 print @__name, "被", child.__name, "继承" 2892 print @__name, "被", child.__name, "继承"
2887 2893
2888-- 将打印: Shelf 被 Cupboard 继承 2894-- 将打印: Shelf 被 Cupboard 继承
@@ -3065,7 +3071,7 @@ class Things
3065```moonscript 3071```moonscript
3066class MoreThings 3072class MoreThings
3067 secret = 123 3073 secret = 123
3068 log = (msg)-> print "LOG:", msg 3074 log = (msg) -> print "LOG:", msg
3069 3075
3070 some_method: => 3076 some_method: =>
3071 log "hello world: " .. secret 3077 log "hello world: " .. secret
@@ -3074,7 +3080,7 @@ class MoreThings
3074<pre> 3080<pre>
3075class MoreThings 3081class MoreThings
3076 secret = 123 3082 secret = 123
3077 log = (msg)-> print "LOG:", msg 3083 log = (msg) -> print "LOG:", msg
3078 3084
3079 some_method: => 3085 some_method: =>
3080 log "hello world: " .. secret 3086 log "hello world: " .. secret
@@ -3101,11 +3107,11 @@ assert @@ == self.__class
3101例如,使用@@从实例方法快速创建同一类的新实例的方法: 3107例如,使用@@从实例方法快速创建同一类的新实例的方法:
3102 3108
3103```moonscript 3109```moonscript
3104some_instance_method = (...)=> @@ ... 3110some_instance_method = (...) => @@ ...
3105``` 3111```
3106<YueDisplay> 3112<YueDisplay>
3107<pre> 3113<pre>
3108some_instance_method = (...)=> @@ ... 3114some_instance_method = (...) => @@ ...
3109</pre> 3115</pre>
3110</YueDisplay> 3116</YueDisplay>
3111 3117
@@ -3145,13 +3151,13 @@ class Something
3145你也可以使用这种语法为一个函数初始化传入对象的字段。 3151你也可以使用这种语法为一个函数初始化传入对象的字段。
3146 3152
3147```moonscript 3153```moonscript
3148new = (@fieldA, @fieldB)=> @ 3154new = (@fieldA, @fieldB) => @
3149obj = new {}, 123, "abc" 3155obj = new {}, 123, "abc"
3150print obj 3156print obj
3151``` 3157```
3152<YueDisplay> 3158<YueDisplay>
3153<pre> 3159<pre>
3154new = (@fieldA, @fieldB)=> @ 3160new = (@fieldA, @fieldB) => @
3155obj = new {}, 123, "abc" 3161obj = new {}, 123, "abc"
3156print obj 3162print obj
3157</pre> 3163</pre>
@@ -3288,7 +3294,7 @@ file = with File "favorite_foods.txt"
3288或者… 3294或者…
3289 3295
3290```moonscript 3296```moonscript
3291create_person = (name, relatives)-> 3297create_person = (name, relatives) ->
3292 with Person! 3298 with Person!
3293 .name = name 3299 .name = name
3294 \add_relative relative for relative in *relatives 3300 \add_relative relative for relative in *relatives
@@ -3297,7 +3303,7 @@ me = create_person "Leaf", [dad, mother, sister]
3297``` 3303```
3298<YueDisplay> 3304<YueDisplay>
3299<pre> 3305<pre>
3300create_person = (name, relatives)-> 3306create_person = (name, relatives) ->
3301 with Person! 3307 with Person!
3302 .name = name 3308 .name = name
3303 \add_relative relative for relative in *relatives 3309 \add_relative relative for relative in *relatives
@@ -3421,7 +3427,7 @@ my_object = {
3421 write: => print "值为:", @value 3427 write: => print "值为:", @value
3422} 3428}
3423 3429
3424run_callback = (func)-> 3430run_callback = (func) ->
3425 print "运行回调..." 3431 print "运行回调..."
3426 func! 3432 func!
3427 3433
@@ -3440,7 +3446,7 @@ my_object = {
3440 write: => print "值为:", @value 3446 write: => print "值为:", @value
3441} 3447}
3442 3448
3443run_callback = (func)-> 3449run_callback = (func) ->
3444 print "运行回调..." 3450 print "运行回调..."
3445 func! 3451 func!
3446 3452
@@ -3500,7 +3506,7 @@ print i -- 将打印 0
3500```moonscript 3506```moonscript
3501i = 100 3507i = 100
3502 3508
3503my_func = (using nil)-> 3509my_func = (using nil) ->
3504 i = "hello" -- 这里创建了一个新的局部变量 3510 i = "hello" -- 这里创建了一个新的局部变量
3505 3511
3506my_func! 3512my_func!
@@ -3510,7 +3516,7 @@ print i -- 打印 100,i 没有受到影响
3510<pre> 3516<pre>
3511i = 100 3517i = 100
3512 3518
3513my_func = (using nil)-> 3519my_func = (using nil) ->
3514 i = "hello" -- 这里创建了一个新的局部变量 3520 i = "hello" -- 这里创建了一个新的局部变量
3515 3521
3516my_func! 3522my_func!
@@ -3524,7 +3530,7 @@ using子句中可以填写多个用逗号分隔名称。指定可以访问和修
3524tmp = 1213 3530tmp = 1213
3525i, k = 100, 50 3531i, k = 100, 50
3526 3532
3527my_func = (add using k, i)-> 3533my_func = (add using k, i) ->
3528 tmp = tmp + add -- 创建了一个新的局部tmp 3534 tmp = tmp + add -- 创建了一个新的局部tmp
3529 i += tmp 3535 i += tmp
3530 k += tmp 3536 k += tmp
@@ -3537,7 +3543,7 @@ print i, k -- 这些已经被更新
3537tmp = 1213 3543tmp = 1213
3538i, k = 100, 50 3544i, k = 100, 50
3539 3545
3540my_func = (add using k, i)-> 3546my_func = (add using k, i) ->
3541 tmp = tmp + add -- 创建了一个新的局部tmp 3547 tmp = tmp + add -- 创建了一个新的局部tmp
3542 i += tmp 3548 i += tmp
3543 k += tmp 3549 k += tmp