【问题标题】:What does this syntax mean (...) [duplicate]这种语法是什么意思(...)[重复]
【发布时间】:2019-02-13 22:29:12
【问题描述】:

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

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}

【问题讨论】:

  • 编辑:我已经看到“组件”默认实现了“渲染”方法。这种语法是否意味着“获取组件对象中的所有内容并在“渲染”功能上添加一些额外的功能?
  • 这不是重复的,因为它不是 ES6 或 JSX,而是一种完全不同的语言,称为 Reason(它最初被正确标记为)。扩展语法在 Reason 中的用途与在 ES6 中(这是灵感的来源)中的用途相似,但并不完全相同,首先也是最重要的,因为它操作的是强的、名义上类型化的记录,而不是动态类型化的对象。

标签: syntax record reason reason-react


【解决方案1】:

这种表示称为传播语法。这是在 ES6 中引入的。

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

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

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

更多详情请访问:Spread Syntax

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多