我是 JavaScript 新手,在阅读了“JavaScript The Good Parts”之后,我想编写一些有趣的代码。我偶然发现了这段我无法理解的代码:
const actions = {
say(sessionId, context, message, cb) {
console.log(message);
cb();
},
merge(sessionId, context, entities, message, cb) {
// Retrieve the location entity and store it into a context field
const loc = firstEntityValue(entities, 'location');
if (loc) {
context.loc = loc;
}
cb(context);
},
error(sessionId, context, error) {
console.log(error.message);
},
['fetch-weather'](sessionId, context, cb) {
// Here should go the api call, e.g.:
// context.forecast = apiCall(context.loc)
context.forecast = 'sunny';
cb(context);
},
};
它是从 wit.ai node.js 客户端截取的。在我的理解中,“actions”是一个对象,“say”、“merge”、“error”和“['fetch-weather']”是保存为没有键的值的函数。
没有保留字“function”怎么可能定义一个函数?
我也无法理解“['fetch-weather']”部分。