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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex