所以我有一个属性,我想为其创建别名。
var default_commands = {}
default_commands['foo'] = "bar";
然后我想要一堆项目别名为“foo”
所以如果我想
default_commands['fu'] = default_commands['foo'];
default_commands['fuu'] = default_commands['foo'];
我该怎么做才能不必全部写出来
我试过:
default_commands['fu','fuu'] = default_commands['foo'];
但这似乎没有用。
最佳答案
["fu", "fuu"].forEach(function (alias) {
default_commands[alias] = default_commands.foo;
});
这不会是单词通常调用的意义上的“别名”:
default_commands.fu = 5;
console.log(default_commands.foo); // still "bar", not 5.
但不清楚您在问题中的意思。
关于javascript - JavaScript 中的属性别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8424969/