私はJavaScriptを初めて使いました。「JavaScript The Good Parts」を読んだ後、私は何かを楽しいものにしたいと思っていました。このJavaScriptオブジェクトで定義されている値は実際に機能していますか?
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クライアントから切り取られます。このコードは、私が理解できないことを切り取ら時に私がstubled。私の理解では、 "アクション"はオブジェクトであり、 "say"、 "merge"、 "error"、 "[fetch-weather]"はキーなしで値として保存される関数です。
"function"という予約語なしに関数を定義する方法はありますか?
私は "['fetch-weather']"部分も理解できません。
それはES6 https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Operators/Object_initializer – jcubic
に短縮表記ですはい、それが最初に見つからないのは残念です。 「 - 」 –