javascript - what is happening in this code and what will be the basic implementation of the following without the "with" keyword -
i going through code base creating multi-platform package management , module system javascript.
i found path of code extracting withing function associated "exports" variable. have attached code snippet below, , on running snippet find "print" object gets extracted function. want know 2 things:-
- how code working?
- can code implemented in more easier way without statement?
var context = { exports: {} }; var fn = (function(args) { with(args) { return function logger() { exports = { print: function(res) { console.log(res); } } } } }); fn = fn(context); fn.call(); context.exports.print('hello world'); //prints hello world
first, evaluating non-string pointless. remove eval
call , use function.
technically, with
statement this:
the
with
statement adds object environment record computed object lexical environment of current execution context. executes statement using augmented lexical environment. finally, restores original lexical environment.
basically, means when assign object identifier exports
, becomes property of args
.
don't this. with
statement has bad performance , not allowed in strict mode. assign property normally.
var fn = function(args) { return function logger() { args.exports = { print: function(res) { console.log(res); } } } };
Comments
Post a Comment