JSRUN 用代码说话
-- 树型打印一个 table,不用担心循环引用
table.print = function(root,depthMax,excludeKey,excludeType)
	assert(type(root)=="table","无法打印非table")
	depthMax = depthMax or 3 -- 默认三层
	local cache = { [root] = "." }
	local depth = 0
	-- local print = env.g_print
	print("{")
	local function _dump(t,space,name)
		local temp = {}
		for k,v in pairs(t) do
			local key = tostring(k)
			if type(k) == "string" then
				key ='\"' .. tostring(k) .. '\"'
			end

			if type(v) == "table" then
				if cache[v] then
					table.insert(temp,space .. "["..key.."]" .." = " .. " {" .. cache[v].."},")
				else
					local new_key = name .. "." .. tostring(k)
					cache[v] = new_key .." ->[".. tostring(v) .."]"

					-- table 深度判断
					depth = depth + 1
					if depth>=depthMax or (excludeKey and excludeKey==k) then
						table.insert(temp,space .. "["..key.."]" .." = " .. "{ ... }")
					else
						local tableStr = _dump(v,space .. (next(t,k) and "|" or " ") .. string.rep(" ",#key<4 and 4 or #key),new_key)
						if tableStr then		-- 非空table
							table.insert(temp,space .. "["..key.."]" .." = " .. "{")
							table.insert(temp, tableStr)
							table.insert(temp,space .."},")
						else 						-- 空table
							table.insert(temp,space .. "["..key.."]" .." = " .. "{ },")
						end
						--table.insert(temp, _dump(v,space .. (next(t,k) and "|" or " " ).. string.rep(" ",#key),new_key))
					end
					depth = depth -1
				end
			else
				local vType = type(v)
				if not excludeType or excludeType~=vType then
					if vType == "string" then
						v = '\"' .. v .. '\"'
					else
						v = tostring(v) or "nil"
					end
					table.insert(temp,space .. "["..key.."]" .. " = " .. v ..",")
					--tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
				end
			end
		end

		return #temp>0 and table.concat(temp,"\n") or nil
	end
	local allTableString = _dump(root, "    ","")
	print(allTableString or "")
	print("}")
	return allTableString
end

local a = {1,2,3,4,5,6,7,8,9}
table.print(a)
single(前端大神) - 2020-07-15 0 人
树型打印一个 table
-- 程序运行完成时一定要有输出语句,本工具才能正确展示运行结果。
Event = {}

function Event:new(event)
	local event = event or {}
	setmetatable(event, self)
	self.__index = self
	return event
end

function Event:Instance()
	if not self.instance then
		self.instance = self:new()
		self._listeners = {}
	end
	return self.instance
end

function Event:addEventListener(listener, eventType, callback)
	-- assert(eventType)
	-- assert(type(listener) == 'table')
	-- assert(type(eventType) == 'string')
	-- assert(type(callback) == 'function')
	print(listener, eventType, callback)
	if not self._listeners[eventType] then
		self._listeners[eventType] = {}
	end
	self._listeners[eventType][listener] = callback
end

function Event:delEventListener(listener, evenType)
	if not evenType then
		for ev, es in pairs(self._listeners) do
			es[listener] = nil
		end
	else
		local es = self._listeners[evenType]
		if es then
			es[listener] = nil
		end
	end
end

function Event:dispatchEvent(eventType, ...)
	if not self._listeners then return end
	local callbacks = self._listeners[eventType]
	if callbacks then
		for listener, func in pairs(callbacks) do
			listener[func](listener, ...)
		end
	end
end

A = {}

function A:a()
	local event = Event:Instance()
	event:addEventListener(self, 'eat', 'getName')	
end

function A:getName()
	print("aaaaaaaaaaaaaaaaaaaaaaaaa")
end

function A:set()
	local event = Event:Instance()
	event:dispatchEvent('eat')
end

A:a()
A:set()






----------------------------------newproxy------------------------
-- function a()
-- 	print("rrrrrrrrrrrrr")
-- end
-- local p = newproxy(true)
-- local mt = getmetatable(p)
-- mt.__gc = function () a() end
-- print("111111111111111111111111111")
-- print(mt ~= nil)

-- local b = newproxy(p)
-- local m = getmetatable(b)
-- print("22222222222222222222222")
-- print(mt == m)

-- local c = newproxy(false)
-- print(getmetatable(c) ~= nil)
single(前端大神) - 2020-07-02 0 人
lua监听事件
-- hello/mytest.lua

local  M  =  {}  -- 私有变量

local  function  test()  print(123)  end

function  M.test1()  test()  end

function  M.test2()  M.test1();  M.test1()  end

return  M  -- 关键
yyltwin(阴阳鱼) - 2019-03-07 0 人
模块打包测试
-- hello/mytest.lua

local  M  =  {}  -- 私有变量

local  function  test()  print(123)  end

function  M.test1()  test()  end

function  M.test2()  M.test1();  M.test1()  end

return  M  -- 关键
yyltwin(阴阳鱼) - 2019-03-07 0 人
模块打包测试
没有了
1/1
没有了