我可以在对象文字中为键加上别名吗? [英] Can I alias a key in an object literal?

查看:54
本文介绍了我可以在对象文字中为键加上别名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
对象文字声明中的自引用

Possible Duplicate:
Self-references in object literal declarations

我有一个对象文字,它用作配置元素并正在查找键.

I have an object literal which is used as a configuration element and is looked up for keys.

customRendering:{
                  key1: func(){....},
                  key2: func(){....}
}

我有一种情况,其中key2key3可以使用相同的功能.有没有一种方法可以为key2key3分配相同的功能,而不必在对象文字之外声明该功能,而不必声明一个额外的键?

I have a situation where key2 and key3 can use the same function. Is there a way to assign the same function to both key2 and key3 without having to declare the function outside the object literal and without having to declare an extra key?

我希望我可以做类似的事情:

I was hoping I could do something like:

key2: key3: func(){....}

推荐答案

一种安全的方法:

我不知道我之前在想什么.如果您可以使用更详细的文字"功能,则可以实例化自定义函数:

A safe way:

I don't know what I was thinking earlier. If you're fine with using a more verbose "literal" you can instantiate a custom function:

o = new function () {
  this.foo = function () { console.log('works'); };
  this.bar = this.foo;
};

这是一个肮脏的讨厌的骇客:

可以在设置对象时使用临时变量存储对函数的引用.在使用闭包之前,请小心使用闭包并调用var,以免污染全局名称空间:

This is a dirty nasty hack:

you could use a temporary variable to store the reference to the function while setting the object. Be careful to use a closure and to call var before using it so that you don't pollute the global namespace:

(function () {
  var o, baz;
  o = {
    foo: baz = function () {console.log('works')},
    bar: baz
  }
  //more code
}());

我之所以称其为肮脏的讨厌的黑客",是因为它会使代码的可读性降低,很难说清楚检查该代码(尤其是对象文字声明是否更长)其中 已设置.

The reason I'm calling this a dirty nasty hack is that it makes the code less readable, it's harder to tell examining this code (especially if the object literal declaration was longer) where baz was set.

最好只将别名写在对象文字之外,这样就可以清楚地看到它是别名.

Better to just write the alias outside the object literal so that it's explicitly visible that it is an alias.

注意:命名的函数格式不起作用:

o = { //THIS WON'T WORK
  foo: function baz() {/* code */},
  bar: baz
}


在对象常量中无法使用共享引用定义别名.

可以使用别名功能,但是不会使用相同的引用:

You can use an aliasing function, but it wont be an identical reference:

o = {
  foo: function...
  bar: function () { return this.foo() } //even better if you use `apply` or `call`
}

共享引用的典型方法是在对象常量之后,这听起来像您想要避免的事情:

The typical way to share a reference is after the object literal, which sounds like what you wanted to avoid:

o = {
  foo: function...
}
o.bar = o.foo;

或者,正如您在问题中指出的(为了完整性),您可以在对象文字之外定义函数:

Alternatively as you pointed out in your question (and for completeness) you could define the function outside of the object literal:

func = function () {/* code */};
o = {
  foo: func,
  bar: func
}

响应@Peter关于从函数返回对象的问题

使用自执行匿名函数是内联实例化对象的另一种方法 ,这将使整个问题无济于事:

Using a self-executing anonymous function is another way of instantiating an object inline, and would make this entire question moot:

o = (function () {
  var o = {
    foo: function () {/*code*/}
  }
  o.bar = o.foo;
  return o;
}());

或者像这样:

var o = (function () {
  var shared = function() { console.log("shared") };
  return {
    foo: shared,
    bar: shared
  }
}());

这篇关于我可以在对象文字中为键加上别名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆