{parameter}和参数之间的区别?

时间:2016-07-26 14:30:04

标签: node.js

我有一个来自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
};

2 个答案:

答案 0 :(得分:5)

它是一个解构运算符,它意味着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}); 

答案 1 :(得分:0)

{ sessionId }是使用EcmaScript 6语法创建对象的代码。它等同于使用旧语法编写{ sessionId: sessionId }。因此send({sessionId}, {text})表示使用两个对象调用send函数,而send(sessionId, text)表示使用(可能)两个字符串调用send函数。