i simply love javascript ... it's so elegant (imagine the quiet sound of lovestruck fanboy sighing in the background).
so, recently i have played with Lua via the löve2d framework (nice!) - and i think Lua is also great. they way i see it, those two languages are very similar.
there are obvious differences, like
- syntax
- problem domain
- libraries
- types (a bit)
but which are the more subtle ones? is there anything a javascript coder would take for granted that works in Lua just slightly different? are there any pitfalls that may not be obvious to the experienced coder of one language trying the other one?
for example: in Lua, arrays and hashes are not separate (there are only tables) - in javascript, they are numerical Arrays and hashed Objects. well, this is one of the more obvious differences.
but are there differences in variable scope, immutability or something like this?
Source: Tips4all, CCNA FINAL EXAM
Some more differences:
ReplyDeleteLua 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.
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.
JS has global and function scope. Lua has global and block scope.
Foreach in JS loops over object properties. Foreach in Lua loops over iterators and is more general.
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 a subtle point that still catches me occasionally sometimes)
In lua, the not/or/and keywords are used in place of !/||/&&. (also syntax but sometimes forget about this too)
In Lua, any type of value can be used to index a table; in JavaScript, object indexes are converted to strings.
To be honest it would be easier to list the things which are common to Javascript and Lua than to list the differences. They are both dynamically-typed scripting languages, but that's about as far as you can go really. They have totally different syntax, different original design goals, different modes of operation (Lua is always compiled to bytecode and run on the Lua VM, Javascript varies), the list goes on and on.
ReplyDeleteJavaScript arrays and objects are closer than you might think. You can use array notation to get at the elements of either of them, and you can add non-numeric indices to arrays. Individual array elements can hold anything, and the array can be sparse. They are nearly identical cousins.
ReplyDeleteA couple of subtle differences that will catch you out at least once:
ReplyDeleteNot equal is spelled ~= in Lua. In JS it is !=
Lua arrays are 1-based - their first index is 1 rather than 0.
Lua requires a colon rather than a period to call object methods. You write a:foo() instead of a.foo() †
† you can use a period if you want, but have to pass the self variable explicitly. a.foo(a) looks a bit cumbersome. See Programming in Lua for details.
Off the top of my head
ReplyDeleteLua ...
supports coroutines
has no restriction to just string/number as key for a table. Everything works.
the error handling is somewhat clumsy. Either you don't handle anything or use the pcall method
I think I read something about differences in the lexical scope and that Lua has the better one.
If I recall correctly regular expression support in lua is limited
I liked this question and the answers provided. Additional reasons the two languages seem more alike than not to me:
ReplyDeleteBoth
assign functions to variables,
can build functions on the fly,
and define closures.
Lua and JavaScript are both prototype base languages.
ReplyDelete