{参数}和参数之间的区别?
我从一个库中获得以下代码,并且我想知道将参数/变量与{}关联起来,还有一个没有它们之间有什么区别?{参数}和参数之间的区别?
行我指的是这个:
send({sessionId}, {text})
下面是完整的代码:
const actions = {
send({sessionId}, {text}) {
// Our bot has something to say!
// Let's retrieve the Facebook user whose session belongs to
const recipientId = sessions[sessionId].fbid;
if (recipientId) {
// Yay, we found our recipient!
// Let's forward our bot response to her.
// We return a promise to let our bot know when we're done sending
return fbMessage(recipientId, text)
.then(() => null)
.catch((err) => {
console.error(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err.stack || err
);
});
} else {
console.error('Oops! Couldn\'t find user for session:', sessionId);
// Giving the wheel back to our bot
return Promise.resolve()
}
},
// You should implement your custom actions here
// See https://wit.ai/docs/quickstart
};
这是一个解构运算符,它表示send()
接受两个参数:一个带有sessionId
键的对象和一个带有text
键的对象。
有效的调用将看起来有点像这样:
actions.send({sessionId: 42}, {text: "Hello World!"});
它也可以其他的方式!所以你可以这样调用:
let sessionId = 42;
let text = "Hello World!";
// Here it means {sessionId: sessionId}, {text: text}
action.send({sessionId}, {text});
{ sessionId }
是用来创建对象的EcmaScript 6语法的代码。这相当于使用旧语法编写{ sessionId: sessionId }
。所以send({sessionId}, {text})
意味着send
函数被两个对象调用,而send(sessionId, text)
意味着send
函数被调用(可能)两个字符串。
不知道为什么这些问题的答案越来越downvoted ... –
@PatrickRoberts因为答案是错的。该示例中未调用该函数,该函数已定义。 –
@MadaraUchiha也许解释不正确,但要点不是。你的观点也是低估的,你是说你错了吗? –
谢谢,Madara Uchiha!非常感激。 – fobia