Skip to content
Gentilhomme edited this page Apr 18, 2020 · 1 revision

Différence entre lua et javascript

variable declaration

myVar = "foo" -- global var
local foo = "bar" -- local to the current scope

undefined / null

local myVar = nil

if

if (myVar !== 10) {

}
if myVar ~= 10 then

end

local myBool = true
if not myBool then
end

block code

{
// do work here
}
do
-- do work here
end

iteration

const myObject = {
    foo: "bar"
}
for (const [key, value] of Object.entries(myObject)) {
    console.log(key, value);
}
local myObject = {
    foo = "bar"
}

for k,v in pairs(myObject) do
    print(k, v)
end

array

const arr = [1, 2, 3]
for (const value of arr) {
}
const arr = {1, 2, 3}
for _, v of pairs(arr) do
end

-- or
local arrLength = #arr -- use # to get the length of the array
for index = 1, arrLength do
  print (arr[index])
end

ternary and default value

const foo = cond || defaultValue;
const x = cond ? a : b;
local foo = cond or defaultValue
local x = cond and a or b

class method (or object function)

myClass.method()
myClass:method()

concat string

"stringA" + "stringB"
"stringA" .. "stringB"

function

function foo(a, ...)
    local args = {...}

    return 1, 2, 3
end

local a, b, c = foo(10, "blabla", "foo")