這裏去我的代碼可變電流模塊中使用
ModuleName.FunctionName.VariableName
我想知道如果這是適用的,我們都知道,裝載AA功能在其他模塊,你必須使用此代碼:
ModuleName.FunctionName
我在想,如果我給出的代碼是適用的。
這裏去我的代碼可變電流模塊中使用
ModuleName.FunctionName.VariableName
我想知道如果這是適用的,我們都知道,裝載AA功能在其他模塊,你必須使用此代碼:
ModuleName.FunctionName
我在想,如果我給出的代碼是適用的。
您可以在其他模塊中使用變量,但語法不像,因爲函數沒有字段。
舉個例子,考慮這個簡單的模塊foo.lua
:
local M = {}
function M.func()
print("calling func")
end
M.var = 42
return M
注意,類似func()
,變量var
必須是全球性的,或者是私有的模塊。
可以使用類似的方式變var
使用的功能func()
:
local foo = require "foo"
foo.func()
print(foo.var)
輸出:
calling func
42
有你可以做到這一點有兩種方式。
1:
-- message.lua local M = {} function M.message() print("Hello") end return M
您可以撥打上面的模塊插入其他的文件。
-- test.lua local msg = require "message" msg.message()
2:
--message.lua msg = "message"
您可以撥打以上模塊通過dofile
-- test.lua dofile ("/home/django/lua/message.lua") -- you should provide complete path of message.lua print(msg)
函數沒有字段,但表做。所以,你可以做
ModuleName.FunctionName -- a function in ModuleName
ModuleName.VariableName -- a variable in ModuleName
ModuleName.TableName.FieldName -- a field from a TableName which is in ModuleName
注FieldName
本身可以引用表,VariableName
可以是一個函數,表,字符串,數字,協程等