第一次在Node.js中看到像这样声明函数的东西。
简而言之,代码类似于以下内容:
'use strict';
const Actions = {
ONE: 'one',
TWO: 'two',
THREE: 'three'
};
class FunMap {
run(action) {
const map = this;
map[action]();
}
[Actions.ONE] () {
console.log("Performing action one");
}
[Actions.TWO] () {
console.log("Performing action two");
}
[Actions.THREE] () {
console.log("Performing action three");
}
}
var funMap = new FunMap();
funMap.run('one');
funMap.run('two');
funMap.run('three');
上述程序将打印
Performing action one
Performing action two
Performing action three
这种在Node.js / Javascript中使用方括号和字符串常量声明的函数有一个技术名称吗?
这行代码如何将所有这些(通过使用方括号和字符串常量声明的函数)放入FunMap对象的属性函数中?
const map = this;
在 JavaScript 类中,方括号符号
[]
是否引用类本身?
["f" + 1] () { }
),但答案是否定的 - 方括号中的代码会在类定义之前运行。 - joewstoString
方法返回值将被使用。在Node控制台中尝试以下代码:{ [1 + 2]: 3 }
、{ [{}]: "object" }
、{ [{ toString() { return "test" } }]: 3 }
。 - joews