perl
次のように、 のクロージャーがどのように機能するかを問題なく理解しています
use 5.012;
use strict;
use warnings;
sub countdown {
my $start = shift;
return sub { $start-- }
}
my $c10 = countdown(3);
say while( $_ = $c10->() );
私は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 someone rewrite the above to perl ? I know perl a bit - so for me will be extremely useful to understanding JS basics and the constructions like:
(...)() - more precisely
(function(){.....})()
double ((
in the if
if ((!context.initialized__QUERY)) {
or the next
(context)["__dirname"] = something ;;
or
return (module)["filename"] = inputPath;; // why double ;;?
And if someone coul'd suggest me any resource something like: Learning Javascript for perl programmers
- would be very nice ;)
Ps: the JS (shortened) is from here: https://github.com/jbr/sibilant/blob/master/lib/cli.js