Learning Javascript from Lua?

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Learning Javascript from Lua?

Krunal Rao
Hi,

Are you aware of any tutorial explaining Javascript assuming Lua knowledge?

Apparently the opposite is more common :-)
http://phrogz.net/lua/LearningLua_FromJS.html

Failing this, does there exist a Javascript book PIL-style?
(to stay in-topic :P, i.e. brief, clear and to the point!)

KRao


Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Nathan Angelacos
On 9/6/11 6:08 PM, KR wrote:

> Hi,
>
> Are you aware of any tutorial explaining Javascript assuming Lua knowledge?
>
> Apparently the opposite is more common :-)
> http://phrogz.net/lua/LearningLua_FromJS.html
>
> Failing this, does there exist a Javascript book PIL-style?
> (to stay in-topic :P, i.e. brief, clear and to the point!)
>
> KRao
>
>

I found  JavaScript: The Good Parts helpful

http://oreilly.com/catalog/9780596517748


Much of the book is also available in video format:

http://yuilibrary.com/theater/douglas-crockford/


To stay in-topic, I found the book and videos enhanced my understanding
and appreciation of Lua.


Reply | Threaded
Open this post in threaded view
|

RE: Learning Javascript from Lua?

Thijs Schreijer
In reply to this post by Krunal Rao
If you want to use JS for web programming, you probably also need some
jQuery. In that case try this free book
https://github.com/rmurphey/jqfundamentals. With a good lua understanding
you'll get up to speed quickly.


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of KR
> Sent: woensdag 7 september 2011 0:08
> To: [hidden email]
> Subject: Learning Javascript from Lua?
>
> Hi,
>
> Are you aware of any tutorial explaining Javascript assuming Lua
> knowledge?
>
> Apparently the opposite is more common :-)
> http://phrogz.net/lua/LearningLua_FromJS.html
>
> Failing this, does there exist a Javascript book PIL-style?
> (to stay in-topic :P, i.e. brief, clear and to the point!)
>
> KRao
>



Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Max de Bayser
The best Javascript book that I know is "JavaScript: The Definitive Guide" by David Flanagan.
The core language chapters are quite good and the API reference section is very complete.





Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

troy wreford

I'm looking to learn javascript better too. What do you guys miss most from Lua when doing javascript programming?  Metatables? Consistency?

On 2011-09-06 6:36 PM, "Max de Bayser" <[hidden email]> wrote:

The best Javascript book that I know is "JavaScript: The Definitive Guide" by David Flanagan.
The core language chapters are quite good and the API reference section is very complete.





Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Duncan Cross
On Wed, Sep 7, 2011 at 12:13 AM, troy wreford <[hidden email]> wrote:
> I'm looking to learn javascript better too. What do you guys miss most from
> Lua when doing javascript programming?  Metatables? Consistency?

For me, probably lexical scoping.

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Matthew Wild
In reply to this post by troy wreford
On 6 September 2011 19:13, troy wreford <[hidden email]> wrote:
> I'm looking to learn javascript better too. What do you guys miss most from
> Lua when doing javascript programming?  Metatables? Consistency?
>

1) Proper lexical scoping. I was hit by this bug in a real app
recently (despite knowing JS's scoping rules):
http://p.zash.se/SVpl9w.txt (that example works with node.js, replace
sys.puts() with console.log() or whatever environment you want to test
with)

Many of the folk in the office couldn't say correctly what that script
prints when I polled them. They more often guessed it printed out what
the literal Lua translation prints out.

2) I find the concept of 'this' much less clean than Lua's 'self'.
Almost every Javascript library or project has a 'bind' function to
fix a given function's concept of 'this'. I've rarely seen such a
function in Lua.

3) I can live fine without metatables in general, but Javascript is
very prototype-centric, yet makes it incredibly difficult to have one
ordinary object inherit methods from another object. Douglas Crockford
has a decent run-down of it here, though even his approach doesn't
work in all cases (I can't remember which, I hit problems when using
his solution in a real project):
http://javascript.crockford.com/prototypal.html

4) Not being able to use any object/type as a key in an object (ie.
Javascript has no equivalent of Lua tables):

   > var a = {}, b = {}, c = {};
   > a[b] = 1; a[c] = 2;
   > print(a[b]);
   Result: 2

This happens because b and c are implicitly converted to strings when
used as keys, so they both evaluate to the key "[object Object]".

And finally, a picture is worth a thousand words, though I know a
number of you here have seen it already:
https://plus.google.com/111178996415789377552/posts/RRZHnv92VEq

Regards,
Matthew

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Tom Wieland
This is a good resource for advanced javascript topics;
http://ejohn.org/apps/learn/

On Wed, Sep 7, 2011 at 2:21 AM, Matthew Wild <[hidden email]> wrote:

> On 6 September 2011 19:13, troy wreford <[hidden email]> wrote:
>> I'm looking to learn javascript better too. What do you guys miss most from
>> Lua when doing javascript programming?  Metatables? Consistency?
>>
>
> 1) Proper lexical scoping. I was hit by this bug in a real app
> recently (despite knowing JS's scoping rules):
> http://p.zash.se/SVpl9w.txt (that example works with node.js, replace
> sys.puts() with console.log() or whatever environment you want to test
> with)
>
> Many of the folk in the office couldn't say correctly what that script
> prints when I polled them. They more often guessed it printed out what
> the literal Lua translation prints out.
>
> 2) I find the concept of 'this' much less clean than Lua's 'self'.
> Almost every Javascript library or project has a 'bind' function to
> fix a given function's concept of 'this'. I've rarely seen such a
> function in Lua.
>
> 3) I can live fine without metatables in general, but Javascript is
> very prototype-centric, yet makes it incredibly difficult to have one
> ordinary object inherit methods from another object. Douglas Crockford
> has a decent run-down of it here, though even his approach doesn't
> work in all cases (I can't remember which, I hit problems when using
> his solution in a real project):
> http://javascript.crockford.com/prototypal.html
>
> 4) Not being able to use any object/type as a key in an object (ie.
> Javascript has no equivalent of Lua tables):
>
>   > var a = {}, b = {}, c = {};
>   > a[b] = 1; a[c] = 2;
>   > print(a[b]);
>   Result: 2
>
> This happens because b and c are implicitly converted to strings when
> used as keys, so they both evaluate to the key "[object Object]".
>
> And finally, a picture is worth a thousand words, though I know a
> number of you here have seen it already:
> https://plus.google.com/111178996415789377552/posts/RRZHnv92VEq
>
> Regards,
> Matthew
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

임창진(callin)
In reply to this post by Duncan Cross
look  "coffee script". it can help you

2011/9/7 Duncan Cross <[hidden email]>:
> On Wed, Sep 7, 2011 at 12:13 AM, troy wreford <[hidden email]> wrote:
>> I'm looking to learn javascript better too. What do you guys miss most from
>> Lua when doing javascript programming?  Metatables? Consistency?
>
> For me, probably lexical scoping.
>
> -Duncan
>
>



--
임창진(rim.chang.jin)

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Axel Kittenberger
In reply to this post by troy wreford
> I'm looking to learn javascript better too. What do you guys miss most from
> Lua when doing javascript programming?  Metatables? Consistency?

Didn't miss metatables much Object.defineProperty gives the most
important uses. Operator overloading is missing, but don't care that
much.

What I really miss:
* associative tables (using non string objects e.g. tables as keys)
* weak tables (does not stop the GC to delete the item)

As I recently work a lot serverside with "node.js" - which is IMHO an
absoletly great tool² - especially there it possibly could use
cooperative multitasking like Lua yields(). Recently due to my node.js
usage, I miss a developed shell environment for Javascript. Yes one
has it all within node.js, but when you develop a shell script its
non-blocking-callback-structure only hampers.

² While I always say "don't be ideological on programming languages -
the right tool for the right job. I quite have a strong dislike
against  PHP".

'this' is more confusing in Javascript. My code works, but I stilll
didn't really fully understand it and had to fix at one or two places
the this pointer. The string objects are quite akward, the difference
between the string object and actual the string value, e.g. the second
is usually interned on most JS implementations while the former must
not by specs.

This might be a controversial statement, but I recently also reflected
for me. Todays Javascript - Lua relationship reminds me a lot of the
1990ies C - Pascal relationship. The Pascal/Lua actually the more
sane, the nicer looking, better developed language with faster parser,
but restricted to a dedicated community. C/Javascript having the mass
power and big industries behind it.

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Dimiter 'malkia' Stanev
> This might be a controversial statement, but I recently also reflected
> for me. Todays Javascript - Lua relationship reminds me a lot of the
> 1990ies C - Pascal relationship. The Pascal/Lua actually the more
> sane, the nicer looking, better developed language with faster parser,
> but restricted to a dedicated community. C/Javascript having the mass
> power and big industries behind it.

I miss Pascal too. My coding language up to 2000. Started C 1993.

What I miss from Pascal, implementation wise - Turbo/Borland/Inprise is
the Unit systems - binary compiled units that a compiler can load and
get definitions automatically. No need for header files and such.

Also the "with" statement, somewhat the custom arrays range, and few
other things.

But nowadays when I see Pascal code it seems to verbose to me (longer
keywords, etc.) - but I often remind myself - code is there to be read
much more times than written.



Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

steve donovan
On Thu, Sep 8, 2011 at 8:33 AM, Dimiter "malkia" Stanev
<[hidden email]> wrote:
> What I miss from Pascal, implementation wise - Turbo/Borland/Inprise is the
> Unit systems - binary compiled units that a compiler can load and get
> definitions automatically. No need for header files and such.

Yes, the old Unix C model, which IHMO has proved a great pain point in
C++[1]. I note that the Unix founders have moved on, and Go has a
similar compiled-unit system (object files with rich metadata) and so
it compiles very fast. (There are other Pascal influences, like 'var
variable type' order in declarations.)

> Also the "with" statement, somewhat the custom arrays range, and few other
> things.

Oh no, 'with' was not good!  I look back on the Delphi code I wrote
and can no longer tell what field belongs to what in with statements.

Axel is completely right - ultimately it's marketing and mindshare
that determines what wins. Still, JavaScript is not the worst thing
that could have happened at that point in history. And a Lua
background helps tremendously.

Now if I only could understand what this damn word 'this' means ;)

steve d.

[1] C++ compilers aren't really slow, but having to process 20K lines
of headers for a simple program does slow them down a little bit.

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Axel Kittenberger
> Axel is completely right

Ooooh along from that 4 words I can feed upon a few months :)

For me working a while with Lua helped me to accept javascript (which
back at the millenium I completly had rejected)

> Oh no, 'with' was not good

Javascript had 'with'. but it is removed from "strict mode" - probably
with reason, and likely to go wit future versions.
In Lua you can do setfenv/_ENV to simulate with - if you absolutely have to.

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

steve donovan
On Thu, Sep 8, 2011 at 1:49 PM, Axel Kittenberger <[hidden email]> wrote:
> In Lua you can do setfenv/_ENV to simulate with - if you absolutely have to.

In the early drafts of Lua 5.2 there was a similar feature to 'with',
and the problem was that people expected it to work like 'with'.

The trouble comes from any locals that happen to be hanging around ...

do
  _ENV = t
  x = 1
  y = 2
  z = 3
end

Cool, but it will break as soon as there's a local 'y' in some
enclosing scope. Not nice!

VB needed an explicit '.' dot in its with statement, which is one of
the few cool things about VB

Personally, I like solving the problem with a macro which provides
something similar to VB:

https://github.com/stevedonovan/LuaMacro/blob/master/tests/test-with.lua

steve d.

Reply | Threaded
Open this post in threaded view
|

Lua in Pascal (Was: Learning Javascript from Lua?)

Dirk Laurie
In reply to this post by Dimiter 'malkia' Stanev
On Thu, Sep 08, 2011 at 08:33:51AM +0200, Dimiter "malkia" Stanev wrote:
> > This might be a controversial statement, but I recently also reflected
> > for me. Todays Javascript - Lua relationship reminds me a lot of the
> > 1990ies C - Pascal relationship. The Pascal/Lua actually the more
> > sane, the nicer looking, better developed language with faster parser,
> > but restricted to a dedicated community. C/Javascript having the mass
> > power and big industries behind it.
>
> I miss Pascal too. My coding language up to 2000. Started C 1993.
>
I still use Pascal.  Not GNU Pascal, but Free Pascal.  It even has
a lua unit with bindings for the Lua API.

All routines from the C API and auxiliary library mentioned in
the Lua 5.1 reference manual are supported in the lua, lualib
or lauxlib libraries of Free Pascal 2.4.  There is no documentation,
but you can use the Lua manual since the call sequences are basically
identical, except for the following:

Type names:

luaL_Reg    => lua_CFunction
    Most others are obvious, with P used for pointer, e.g.
luaState*   => Plua_State
    or standard, e.g.
void*       => Pointer
int         => Integer
    You can always look at the interface of lauxlib.pas etc
(package fpc-source on Ubuntu) to make sure.

    Some routine names have no L (probably typos):
luaL_dofile         => lua_dofile
luaL_dostring       => lua_dostring
luaL_getmetatable   => lua_Lgetmetatable

    It's annoying that Free Pascal has long strings, but no way of
continuing a string constant across line boundaries.

Minimal usage example:

--[[
uses lua, lualib, lauxlib;

var L: Plua_State;

begin
    L := lua_open(); luaL_openlibs(L);
    lua_dostring(L,
'for k in string.gmatch("a,b,c,d,e,f,g,h","([^,]+),") do print(k) end'
);
    lua_close(L);
end.
--]]

Simply fpc that and run the executable.

Dirk

Reply | Threaded
Open this post in threaded view
|

Re: Learning Javascript from Lua?

Mark Hamburg
In reply to this post by Matthew Wild
On Sep 6, 2011, at 5:21 PM, Matthew Wild wrote:

> And finally, a picture is worth a thousand words, though I know a
> number of you here have seen it already:
> https://plus.google.com/111178996415789377552/posts/RRZHnv92VEq

And if you read JavaScript: The Good Parts, you discover that much of the book is dedicated to what's wrong with JavaScript. My reaction while reading it was that Lua managed to have most of the good parts while avoiding the bad parts. (I could see use for separate NULL and no-value/undefined values in Lua with respect to things like holes in arrays, __index tables for inherited values, but that might just be kicking the problem down the road.)

Mark