I have an object in my class like so:
this.options = {
      a: 1,
      b: 2,
      ...
 };
 I just wondered if there was a way to "attach" the options object to the current scope in Javascript, such that I can refer to a and b directly (instead of this.options.a and this.options.b). Something like:
attach(this.options);
 var myVariable = a + 3; // 4
 detach(this.options);
 or
with( this.options, {
    // code here
  var myVariable = a + 3; // 4
 });
 In the above, the attach and with have the effect of taking all of the children of this.options and attaching them to the current scope so they are accessible via a instead of this.options.a. (The with version simply detaches this.options once you're done, enclosing the potentially-damaging attaching to within that scope).
As long as it works in the Spidermonkey/Mozilla engine, I don't mind if it's not portable.
I only mention this because of curiosity & laziness: all the nested names are getting annoying to type out, and I know that in the R language this is possible (in fact, the syntax in the snippets above is almost exactly what you'd write in R to achieve this).
No comments:
Post a Comment