Prevent JavaScript closure from inheriting scope
I am looking for a fancy way to prevent a closure from inheriting surrounding scrope. For example: let foo = function(t){ let x = 'y'; t.bar = function(){ console.log(x); // => 'y' }); }; there are only two ways I know of preventing sharing scope: (1) Use shadow variables: let foo = function(t){ let x = 'y'; t.bar = function(x){ console.log(x); // => '?' }); }; (2) Put the function body somewhere else: let foo = function(t){ let x = 'y'; t.bar = createBar(); }; My question is - does anyone know of a 3rd way to prevent closures from inheriting scope in JS? Something fancy is fine. The only