简体   繁体   English

Javascript附加对象语法糖?

[英]Javascript attach an object syntactic sugar?

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 ). 我只是想知道是否有一种方法可以将options对象“附加”到Javascript中的当前范围,这样我就可以直接引用ab (而不是this.options.athis.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 . 在以上内容中, attachwith的作用是获取this.options所有this.options并将其附加到当前作用域,以便可以通过a代替this.options.a来访问它们。 (The with version simply detaches this.options once you're done, enclosing the potentially-damaging attaching to within that scope). (一旦完成, with版本会简单地分离this.options ,将可能损坏的this.options包含在该范围内)。

As long as it works in the Spidermonkey/Mozilla engine, I don't mind if it's not portable. 只要它能在Spidermonkey / Mozilla引擎中运行,我都不介意它是否不可移植。

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). 我只是出于好奇和懒惰而提到此问题:所有嵌套名称都令人讨厌地键入,而且我知道在R语言中这是可能的(事实上,上面的摘录中的语法几乎就是您所写的在R中实现这一目标)。

JavaScript has a with statement that works similar to what you describe: JavaScript的with语句的工作方式与您描述的类似:

with (this.options) {
    var myVariable = a + 3;  // here a is actually this.options.a
}

However as you can see at the MDN article I linked to there are some cons to using it, because, eg, of potential ambiguity. 但是,正如您在我所链接的MDN文章中所看到的那样,使用它存在一些弊端,例如,由于可能存在歧义。 In the example above, a could be a property of this.options , or it could be a variable defined elsewhere, and looking just at that block there is no way to tell. 在上面的示例中, a可以是this.options的属性,也可以是在其他地方定义的变量,仅查看该块就无法分辨。

with is not recommended (and is forbidden in strict mode ). with不建议(并在禁止严格模式 )。

There is a safe alternative though: create a variable that references (again in your example) this.options : 不过,还有一个安全的选择:创建一个变量(再次在您的示例中) this.options

var o = this.options;

var myVariable = o.a + 3;

It's not quite as short as typing a on its own, but it's shorter than repeating this.options everywhere. 它不像this.options键入a那样短,但是比在各处重复this.options短。

You could wrap you object in another object, with attach and detach methods as well as an internal cache to store the actual data object. 您可以使用attachdetach方法以及内部缓存来存储实际数据对象,从而将对象包装在另一个对象中。 then have the attach and detach methods provide data as this inside the callback: 然后让attachdetach方法提供data作为this回调中:

function TheObject(data){
    var that = this;

    this.data = data;

    this.attach = function(callback){
        callback.apply(that.data);
    }

}

//wrap the object
var myobj = new TheObject({
    a: 1,
    b: 2
});


myobj.attach(function(){
    //"this" in here is the "data" in the object.
    //this.a
    //this.b
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM