首页 > 解决方案 > 这个语法是什么意思(...)

问题描述

我正在着手进行理性反应。在以下代码中:

let component = ReasonReact.statelessComponent("Component3");
let make = (~name, _children) => {
  ...component,
  render: self => <input type_="checkbox" />,
};

我不明白第 3 行 (...) 的含义。当我删除它时,我收到一条错误消息:

 The record field component can't be found.

  If it's defined in another module or file, bring it into scope by:
  - Annotating it with said module name: let baby = {MyModule.age: 3}
  - Or specifying its type: let baby: MyModule.person = {age: 3}

标签: syntaxrecordreasonreason-react

解决方案


该表示称为扩展语法。这是在 ES6 中引入的。

MDN 文档中的定义和示例。链接在底部。

扩展语法允许在预期零个或多个参数(对于函数调用)或元素(对于数组字面量)的地方扩展诸如数组表达式或字符串之类的可迭代对象,或者在零个或多个参数的地方扩展对象表达式需要键值对(用于对象文字)

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

console.log(sum.apply(null, numbers));
// expected output: 6

更多详细信息:传播语法


推荐阅读