Skip to main content

subtle differences between javascript and Lua


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: Tips4allCCNA FINAL EXAM

Comments

  1. 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.
    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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. JavaScript 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.

    ReplyDelete
  4. A couple of subtle differences that will catch you out at least once:


    Not 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.

    ReplyDelete
  5. Off the top of my head

    Lua ...


    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

    ReplyDelete
  6. I liked this question and the answers provided. Additional reasons the two languages seem more alike than not to me:

    Both
    assign functions to variables,
    can build functions on the fly,
    and define closures.

    ReplyDelete
  7. Lua and JavaScript are both prototype base languages.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.