您能打电话给“要求"吗?在变量上? [英] Can you call "require" on a variable?

查看:85
本文介绍了您能打电话给“要求"吗?在变量上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个lua文件,它将需要另一个lua文件才能运行,但是我无法对文件名进行硬编码.我可以在变量上使用require函数吗,还是需要找出一种替代的方法来做我的工作?

I have a lua file that will require another lua file to run but I can't hard code the file name. Can i use the require function on a variable or do i need to figure out an alternate approach to what i am doing?

例如

local path = "mypath.txt"

local level = require path

推荐答案

是的,可以. require "module"只是require("module")的语法糖,仅当调用带有字符串或表构造函数的单个参数的函数时,该语法才起作用.以require(path)的形式使用正确的调用,它将起作用.

Yes, you can. require "module" is just a syntactic sugar for require("module") that only works if call a function with single argument that is a string or table constructor. Use proper call in form of require(path) and it will work.

这篇关于您能打电话给“要求"吗?在变量上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆