最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Any way to define ALL variables within function as property of this? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Dec 8, 2016 at 22:29 ThomasReggi asked Dec 8, 2016 at 22:21 ThomasReggiThomasReggi 59.7k97 gold badges259 silver badges459 bronze badges 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
Add a ment  | 

2 Answers 2

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论