跳转到内容

模組:HelloWorld

维基百科,自由的百科全书

这是本页的一个历史版本,由Great Brightstar留言 | 贡献2014年8月8日 (五) 11:36 建立内容为“my_object = {}; --維基百科上的Lua模塊必須在開頭定義一個變量,使參數可從外面存取。 --變量的...”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

my_object = {};     --維基百科上的Lua模塊必須在開頭定義一個變量,使參數可從外面存取。
                    --變量的名稱沒有限制,且可以含有數據。

my_object.hello = function( frame )     --Add a function to "my_object".  
                                        --Such functions are callable in Wikipedia
                                        --via the #invoke command.
                                        --"frame" will contain the data that Wikipedia
                                        --sends this function when it runs. 
    
    local str = "Hello World!"  --Declare a local variable and set it equal to
                                --"Hello World!".  
    
    return str    --This tells us to quit this function and send the information in
                  --"str" back to Wikipedia.
    
end  -- end of the function "hello"

return my_object    --All modules end by returning the variable containing its
                    --functions to Wikipedia.

-- Now we can use this module by calling {{#invoke: HelloWorld | hello }}.
-- Note that the first part of the invoke is the name of the Module's wikipage,
-- and the second part is the name of one of the functions attached to the 
-- variable that you returned.

-- The "print" function is not allowed in Wikipedia.  All output is accomplished
-- via strings "returned" to Wikipedia.