我有一个lib中的以下代码,我想知道用{}括起来的参数/变量和没有它们的参数/变量有什么区别?
我指的是这条线:

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});

关于node.js - {parameter}和参数之间的区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38592685/

10-12 04:01
查看更多