javascript - Explaining JS closures in the terms of perl -


i understand without problems how perl's closures works, next one

use 5.012; use strict; use warnings;  sub countdown {         $start = shift;         return sub { $start-- } }  $c10 = countdown(3); while( $_ = $c10->() ); 

i'm trying understand next piece of javascript:

var runinsandbox = (function(js, inputpath) {    (function() {     if ((!context.initialized__query)) {       return createcontext();     };   })();   (function() {     if (typeof(inputpath) !== 'undefined') {       (process.argv)[1] = inputpath;;       (context)["__dirname"] = path.dirname(inputpath);;       return (module)["filename"] = inputpath;;     };   })();   return vm.runincontext(js, context, "sibilant"); }); 

no chance! :( please can rewrite above to perl ? know perl bit - me extremely useful understanding js basics , constructions like:

(...)() - more precisely (function(){.....})() 

double (( in if

    if ((!context.initialized__query)) { 

or next

      (context)["__dirname"] = ;; 

or

       return (module)["filename"] = inputpath;; // why double ;;? 

and if coul'd suggest me resource like: learning javascript perl programmers - nice ;)

ps: js (shortened) here: https://github.com/jbr/sibilant/blob/master/lib/cli.js

i'm not extremely well-versed perl closures, @ least try demystify you.

the form:

(function(...) {  ... })(); 

is self-invoked anonymous function1. means write out anonymous function, , invoke immediately. done encapsulation2. example, if end creating bunch of variables, don't want pollute global namespace, can put inside anonymous, self-invoked function. however, in case don't see why first invocation necessary @ all, since it's checking flag or something. stranger return inside self-invoked functions. aren't being assigned anything. hazard guess createcontext() initializes context variable, return in there useless. same goes following:

return (module)["filename"] = inputpath;; 

as far double (( , )), seem largely unnecessary , i'm not sure why author put in there. example:

if ((!context.initialized__query))  

isn't different from:

if (!context.initialized__query)  

also, parentheses in following unnecessary, double semicolons:

(context)["__dirname"] = ;; 

honestly, looks poorly-written javascript, or perhaps javascript autogenerated (this case).

you rewrite so:

var runinsandbox = function(js, inputpath) {      if (!context.initialized__query) {        createcontext();     };      if (typeof inputpath !== 'undefined') {        process.argv[1] = inputpath;        context["__dirname"] = path.dirname(inputpath);        module["filename"] = inputpath;     };      return vm.runincontext(js, context, "sibilant"); }; 

notes:

  1. in perl, sub { ... }->().
  2. in perl, 1 use { $var; ... } instead of sub { $var; ... }->() , do { $var; ...; expr } instead of sub { $var; ...; return expr; }->().

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -