3

I know this may sound a little absurd, but I'm looking for a way to define every variable within a function as a property of this. I'm looking for any hack, any way possible to be able to have some way to track variables within a function (i.e. add them to the this object) without having to actually preface every single variable definition with this.. Is there a way? Is this possible with Proxy?

function () {
  // declare a variable
  var hello = 'hi'
  return this
}

let {hello} = function()
console.log(hello) // hi

For example this works:

function hi () { this.hello = true; return this }
hi.bind({})() // { hello: true }

What I want is a way to have all variables defined within hi to be added to the this object when they are defined.

2
  • Isn't this a singular nature? A reference to the function owner? Any variable can reference this, but it references one thing only. It can be changed by .call or .apply.
    – zer00ne
    Commented Dec 8, 2016 at 22:27
  • 1
    You can't do that.
    – SLaks
    Commented Dec 8, 2016 at 22:27

2 Answers 2

11

You are looking for the worst hack imaginable? Sure, everything is possible:

function example () {
  with(horrible(this)) {
    var hello = 'hi';
  }
}
var x = new example;
x.hello; // 'hi'


function horrible(target) {
  return new Proxy(target, {
    has() { return true; }, // contains all variables you could ever wish for!
    get(_, k) { return k in target ? target[k] : (1,eval)(k) }
  });
}

The proxy claims to contain all names that could ever be used as a variable in the with scope. This basically leads to all assignments of undeclared or var-declared variables create properties on the target (unless you use let or const, they'll be truly local to the block scope).
However, for variable lookup everything that is not a target property will be resolved in the global scope (using global eval) as the original scope cannot be retained when the proxy said it can deliver all variables.

13
  • Much cleaner that my answer! But references to outer variables won't work, e.g console.log('foo')
    – Oriol
    Commented Dec 8, 2016 at 22:55
  • @Oriol I was just fixing that :-)
    – Bergi
    Commented Dec 8, 2016 at 22:58
  • 1
    @Bergi any way to include vars declared with let and const? Commented Dec 9, 2016 at 0:02
  • 1
    @ThomasReggi No, but that's actually quite useful if you need some variables that should not become properties. If you really want complete control, get the function's code (via .toString) and do static analysis or transpilation.
    – Bergi
    Commented Dec 9, 2016 at 0:29
  • @Bergi check this astexplorer.net/#/fIw4JAexWk/2 credit => twitter.com/drewml Commented Dec 9, 2016 at 0:47
3

You can, kind of. But it's a very very dirty hack which requires you to

  • Wrap the code inside a with statement, which has performance costs, is bad practice, and is not allowed in strict mode.
  • Use evil eval to get the values of the variables.
  • There can be false positives. Only var variables are detected, but not let or const ones.

function func() {
  // The proxy will detect the variables
  var vars = [];
  var proxy = new Proxy({}, {
    has(target, property) {
      vars.push(property);
    }
  });
  with(proxy) {
    // Place your code here
    var hello = 'hi';
  }
  // Assign the variables as properties
  for (var property of vars)
    this[property] = eval(property);
  return this;
}
let {hello} = func.call({});
console.log(hello) // hi

Not the answer you're looking for? Browse other questions tagged or ask your own question.