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

Lua和Javascript差异对比的更多相关文章

  1. Lua与javascript的差异

    Lua与javascript的差异 2010-03-08 Lua模拟器js方案 1.语法级模拟 lua与js语言差异 1.1注释 js 为//,lua为--. 1.2变量 js利用var来声明全局变量 ...

  2. 16.VUE学习之-v-show的使用与v-if的差异对比

    v-show的使用与v-if的差异对比 相同点: 都可以达到隐藏和显示的效果. 不同点: v-show 会用display:none 来隐藏元素节点,推荐使用这种方式 v-if 会移除节点,可以配合v ...

  3. vue(element)中使用codemirror实现代码高亮,代码补全,版本差异对比

    vue(element)中使用codemirror实现代码高亮,代码补全,版本差异对比 使用的是vue语言,用element的组件,要做一个在线编辑代码,要求输入代码内容,可以进行高亮展示,可以切换各 ...

  4. Atitit 硬件 软件 的开源工作 差异对比

    Atitit 硬件 软件 的开源工作 差异对比 1.1. 模块化,标准化,以及修改的便捷性1 1.2. 生产和发布成本 1 1.3.   3. 入行门槛搞2 1.4.  在软件业极度发达的今天,任何具 ...

  5. SeaJS 与 RequireJS 的差异对比

    这篇文章主要介绍了SeaJS 与 RequireJS 的差异对比,本文主要对CMD规范和AMD规范的弊端做了对比,并做出了一个总结,需要的朋友可以参考下 “历史不是过去,历史正在上演.随着 W3C 等 ...

  6. Python 数据库之间差异对比

    参考资料: Python 集合(set)   此脚本用于两个数据库之间的表.列.栏位.索引的差异对比. cat oracle_diff.py #!/home/dba/.pyenv/versions/3 ...

  7. 016——VUE中v-show的使用与v-if的差异对比

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Python自动化运维——文件内容差异对比

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:difflib 安装:Python版本大于等于2.3系统自带 功能:对比文本之间的差异,而且支持输出可读性比 ...

  9. 类似于SVN的文档内容差异对比工具winmerge

    原文:http://www.jianshu.com/p/99282a4f3870 https://sourceforge.net/projects/winmerge/?source=typ_redir ...

随机推荐

  1. webstom 配置git 后左侧菜单栏配色调整

    ws配置了git进行版本管理,但是最近改了主题,发现左侧列表的文件名的颜色都是一个颜色了.我想要的是,对未提交的文件用颜色区分,新建的,修改的,冲突的分别设置不同的颜色,不知在哪里能设置,求助 老规矩 ...

  2. select unit_timestamp(); 和select unit_timestamp("1970-1-1 08:00:00")和 select from_unixtime(1)

    偶然看到MySQL的一个函数 unix_timestamp(),不明就里,于是就试验了一番. unix_timestamp()函数的作用是返回一个确切的时间点的UNIX时间戳,这个Unix时间戳是一个 ...

  3. JQ异步调用

    AjaxGet请求方式: <script type="text/javascript"> $.ajax({ type: "GET", dataTyp ...

  4. asp.net mvc Remote远程验证

    1.Model实体 /// <summary> /// 课程编号 /// </summary> [MaxLength()] [Remote("IsUnique_Ava ...

  5. 学习笔记7_Java_day11_JSP原理(5)

    4. jsp原理(理解) * jsp其实是一种特殊的Servlet > 当jsp页面第一次被访问时,服务器会把jsp编译成java文件(这个java其实是一个servlet类) > 然后再 ...

  6. OC - 2.OC基础知识介绍

    一.基础语法 1> OC语言和C语言 C语言是面向过程的语言,OC语言是面向对象的语言 OC语言继承了C语言,并增加了面向对象的思想 以下内容只介绍OC语言与C语言的不同之处 2> 关键字 ...

  7. weewwe

    http://blog.csdn.net/u013073524/article/details/25912891

  8. Windows下ANSI、Unicode、UTF8字符编码转换

    主意:输入字符串必须是以'\0'结尾,如果输入字符串没有以'\0'结尾,请手动设置,否则转换会有错误. unsigned int EncodeUtil::AnsiToUcs2( char* pAnsi ...

  9. Graphviz使用简介(中文乱码的问题)

    Graphviz使用简介 graphviz是基于dot语言的绘图工具,可以画有向图.无向图.关系图.目录图.流程图等.具体作用可见它的官方网站 一些参考的网址: http://www.open-ope ...

  10. Wap站总结一

    前段时间负责了公司的wap站前端工作,目前wap站的基础及较为复杂的几张页面都已经出来,现根据自己的一些经验,贴出部分心得,希望对现在或者以后可能会接触到Wap站的一些人有些帮助 一.本次WAP网站的 ...