Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

Re^2: [OT] Perl / Computer Science Science Fair Projects

by Anonymous Monk
on Sep 09, 2008 at 16:40 UTC ( #710141=note: print w/ replies, xml ) Need Help??


in reply to Re: [OT] Perl / Computer Science Science Fair Projects
in thread [OT] Perl / Computer Science Science Fair Projects

Unfortunately in JavaScript all variables are scoped to the nearest function call. So a literal translation does something very different:
for (var i = 0; i < some_array.length; i++) { // Declaring this in the loop creates a one variable for // all instances of the loop. var x = some_array[i]; closures.push(function () { // Do something using x. I'll just return it. return x; }); } // All closures now point at the last element.
How annoying.
One of the joys of using the Mozilla Javascript environment exclusively is that that some of these issues have been dealt with. Javascript 1.7 introduced the let keyword that deals with this scoping issue.
for (var i = 0; i < some_array.length; i++) { // Declaring this in the loop creates a one variable for // all instances of the loop. let x = some_array[i]; closures.push(function () { // Do something using x. I'll just return it. return x; }); } // now works as expected


Comment on Re^2: [OT] Perl / Computer Science Science Fair Projects
Select or Download Code
Re^3: [OT] Perl / Computer Science Science Fair Projects
by tilly (Archbishop) on Sep 09, 2008 at 18:01 UTC
    I ran across that, and immediately decided that there was so little backwards compatibility that I was going to forget it again.

    Also you can't use the let keyword unless you've explicitly declared that you're using version 1.7.

    <script type="application/javascript;version=1.7"> ... </script>
    which strikes me as a very annoying design choice.
        It is certainly similar, and was done for a similar reason.

        However the Perl version had better cause to do it (there is popular code in the wild which conflicts with Perl 5.10 features, whereas worries about functions named "let" are more theoretical), and did it with a language mechanism that is used by a lot of third party modules.

        But there are other significant differences. The JavaScript version has horrible action at a distance. If my library uses let, you have to know that when you load it in a different file. How to do it is not obvious. If I have multiple JavaScript snippets, I have to include that feature in each one. If I wish to inline some JavaScript in a tag, there is (as far as I know) absolutely no way for me to say that I want features turned on. (Yes, in autogenerated code it sometimes does make sense to put complex JavaScript in tags.) Furthermore if I'm a novice bitten by JavaScript's unusual scoping rule, there is no easy way for me to discover that workaround.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://710141]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-04-18 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (466 votes), past polls