Lua和Javascript差异对⽐
Lua模拟器js⽅案
1.语法级模拟
lua与js语⾔差异
1.1注释
js 为//,lua为--.
1.2变量
js利⽤val来声明全局变量不存在局部变量,lu a则不需要直接定位则为全局变量,local声明则为局部变量。
1.3运算符
js
+ - * / % ++ --
= += -= *= /= %=
⽀持字符串 +
txt1 = "what a very";
txt2 = "nice day";
txt3 =txt1 " " +txt2;
打印txt3输出结果为"what a very nice day".
规则:
把数字与字符串相加,结果将成为字符串.
lua
⼆元:+ - * / ^ %
⼀元:-(负号)
lua字符串拼接为..
如"Hello ".."World"拼接成Hello World
1.4关系操作符
js关系操作符
==  ===(全等) != > < >= <=
lua关系操作符
< > <= >= == ~=(不等于)
1.5 逻辑运算符
js
&& || !
lua
and or not
1.6 If ...Else语句
js 类c
if else
lua
if then else
if  then
elseif then
else
end
⼀定要有end
1.7 Switch语句
lua不⽀持Switch 语句
1.8 消息框
js
警告框 alert("⽂本")
确认框 prompt("⽂本","默认值")
lua
扩展⽀持警告框和确认框
1.9  函数
js
function 函数名(参数)
{
  代码...
}
js带{}类 c
lua
function 函数名( 参数)
end
lua类vb 脚本
2.0 For 循环
js:类c
for (i=0;i<=10;i++)
{
document.write("The number is " + i)
document.write("<br />")
}
lua:分两种数字型For 和泛型For
数字型For:
for var= exp1,exp2,exp3 do
<;执⾏体>
end
var从exp1变化到exp2,step为exp3递增
不指定exp3默认为1
for i =1,100 do
print(i)
end
for i =1,100,2 do
print(i)
end
泛型For
泛型For循环通过⼀个迭代器(iterator)函数来遍历所有值:
--打印数组a 的所有值
for i,v in pairs(a) do print(v) end
Lua基础库提供了ipairs,这是⼀个⽤于遍历数组的迭代器函数。
在每次循环中i会被赋予⼀个索引值,同时v会被赋予⼀个对应于
该索引的数组元素值。
---打印table t中所以的key
for k in pairs(t) do print(k) end
2.1 While循环
js: 类c
while (变量<=结束值)
{
需执⾏的代码
}
lua:
i =1;
while a[i] do
print(a[i])
i = i+1;
end
同时lua还⽀持repeat:⽀持repeat-until语句实现循环.
repeat:
line = io.read()
until line~=""
print(line)
上⾯的代码:读取line直到line不为""的时候结束,并打印此line的值。
2.2  Break 和 Continue
js:类c
有两种可以⽤在循环中的语句:break 和 continue
Break
break 命令可以终⽌循环的运⾏,然后继续执⾏循环之后的代码(如果循环之后有代码的话)。
Code⽰例:
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
lua字符串转数组{
if (i==3){break}
document.write("The number is " + i)
document.write("<br />")
}
</script>
Continue
continue 命令会终⽌当前的循环,然后从下⼀个值继续运⾏。
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3){continue}
document.write("The number is " + i)
document.write("<br />")
}
</script>
Lua:
⽀持break,但不⽀持continue.
local i =1
while a[i] do
if a[i] == v then break end
i = i +1
end
2.In 声明
js:⽤In 声明专门遍历数组内的元素。
for ... in 循环中的代码每执⾏⼀次,就会对数组的元素或者对象的属性进⾏⼀次操作。
语法:
for (变量 in 对象)
{
在此执⾏代码
}
Code:
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
Lua:很简单直接⽤泛型的For取代即可.
下⾯转⾃stackoverflow回答:
Some more differences:
Lua has native support for .
Lua  between types for any comparison operators. In JS, only '===' and '!==' don't type juggle.
Lua has an exponentiation operator (^); JS doesn't. JS has many more operators, including the ternary conditional operator (?:), increment/decrement, bitwise operators, type operators (typeof and instanceof), additional assignment operators and additional
comparison operators.
In JS, the equals and not equals operators are of lower precedence than less than et al. In Lua, all comparison operators are the . Lua supports .
Lua supports . While it isn't yet standard in Javascript, Mozilla's JS engine (and Opera's, to an extent) has supported a similar feature since JS 1.7 (available as part of Firefox 2) under the name "". Destructuring in JS is more general, as it can be used in contexts other than assignment, such as  and .
has been a proposed addition to ECMAScript (the language standard behind Javascript) for awhile. In Lua, you can .
In Lua, you can  with getfenv & setfenv.
In JS, all functions are variadic. In Lua, functions must be .
Foreach in JS loops over object properties.  in Lua (which use the keyword for) loops over iterators and is more general.
JS has global and function scope. Lua has . C ontrol structures (e.g. if, for, while) introduce new .
Due to differences in scoping rules, a closure's referencing of an outer variable (called "upvalues" in Lua parlance) may be handled differently in Lua and in Javascript. This is most commonly experienced with , and catches some people by surprise. In Javascript, the body of a for loop doesn't introduce a new scope, so any functions declared in the loop body all reference the . In Lua, each iteration of the for loop creates new local variables for each loop variable.
local i='foo'
for i=1,10 do
-- "i" here is not the local "i" declared above
...
end
print(i) -- prints 'foo'
The above code is equivalent to:
local i='foo'
do
local _i=1
while _i<10 do
local i=_i
...
_i=_i+1
end
end
print(i)
As a consequence, functions defined in separate iterations have different upvalues for each referenced loop variable. See also Nicolas Bola's answers to  and "", and "".
Integer literals in JS can be in octal.
JS has explicit Unicode support.
In lua, ~ is used in place of !. (as in, if foo ~= 20 then ... end) (technically syntax, but it's easily overlooked and causes subtle bugs).
In lua, the not/or/and keywords are used in place of !/||/&& (also syntax but also easily forgotten).
In Lua, any type of value (except nil and NaN) can be used to index a table; in JavaScript, object index
es are converted to strings.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。