lua

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Lua

Lua

A powerful, efficient, lightweight, embeddable scripting language.
一种功能强大、高效、轻量级的可嵌入脚本语言。

When to Use

使用场景

  • Game Development (Roblox, WoW, Love2D)
  • Embedded Systems
  • Configuration scripting (Neovim, Nginx)
  • Extending applications
  • 游戏开发(Roblox、WoW、Love2D)
  • 嵌入式系统
  • 配置脚本编写(Neovim、Nginx)
  • 扩展应用程序功能

Quick Start

快速入门

lua
print("Hello, World!")

function factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end
lua
print("Hello, World!")

function factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end

Core Concepts

核心概念

Tables

表(Tables)

The only complex data structure in Lua. Used as arrays, dictionaries, sets, and objects.
lua
t = { key = "value", [1] = "first" }
print(t.key)
Lua中唯一的复杂数据结构,可作为数组、字典、集合和对象使用。
lua
t = { key = "value", [1] = "first" }
print(t.key)

Metatables

元表(Metatables)

Allow changing the behavior of tables (e.g., operator overloading, inheritance).
允许修改表的行为(例如运算符重载、继承)。

Indices

索引

Arrays are 1-indexed (start at 1, not 0).
数组采用1索引(从1开始,而非0)。

Best Practices

最佳实践

Do:
  • Use
    local
    variables by default (performance and scope)
  • Use standard libraries where possible
  • Understand table length operator
    #
    behavior with holes
Don't:
  • Pollute the global namespace
  • Ignore
    nil
    (undefined variables are
    nil
    )
建议
  • 默认使用
    local
    变量(提升性能并控制作用域)
  • 尽可能使用标准库
  • 理解存在空洞时表长度运算符
    #
    的行为
禁忌
  • 污染全局命名空间
  • 忽略
    nil
    (未定义的变量值为
    nil

References

参考资料