Lua和Javascript差異對比


Lua模擬器js方案

1.語法級模擬
lua與js語言差異

1.1注釋 
js 為//,lua為--.

1.2變量
js利用val來聲明全局變量不存在局部變量,lua則不需要直接定位則為全局變量,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++)
{
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.3 For...In 聲明
js:用For...In 聲明專門遍歷數組內的元素。 
For...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 coroutines.
  • Lua doesn't convert 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 same precedence.
  • Lua supports tail calls.
  • Lua supports assignment to a list of variables. 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 assignment". Destructuring in JS is more general, as it can be used in contexts other than assignment, such as function definitions & calls and loop initializers.Destructuring assignment has been a proposed addition to ECMAScript (the language standard behind Javascript) for awhile.
  • In Lua, you can overload operators.
  • In Lua, you can manipulate environments with getfenv & setfenv.
  • In JS, all functions are variadic. In Lua, functions must be explicitly declared as variadic.
  • Foreach in JS loops over object properties. Foreach in Lua (which use the keyword for) loops over iterators and is more general.
  • JS has global and function scope. Lua has global and block scope. Control structures (e.g. ifforwhile) introduce new blocks.

    • 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 closures in for loops, 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 same outer variables. 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 Implementation of closures in Lua? and "What are the correct semantics of a closure over a loop variable?", and "The Semantics of the Generic for".

  • 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 indexes are converted to strings.

 

更多參考:http://stackoverflow.com/questions/1022560/subtle-differences-between-javascript-and-lua


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM