gpt4 book ai didi

javascript - JS ES6 类定义 : Example on Preact homepage: I've never seen this

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:08 25 4
gpt4 key购买 nike

下面的例子是在Preact homepage .我想知道类花括号 {} 中如何/为什么有等号 = 赋值和分号 ;。我已经用谷歌搜索了几分钟,但似乎无法弄清楚。

这是 TypeScript 还是其他一些花哨的 JS 表亲?大括号看起来像常规赋值,而不是类定义。

export default class TodoList extends Component {
state = { todos: [], text: '' };
setText = e => {
this.setState({ text: e.target.value });
};
addTodo = () => {
let { todos, text } = this.state;
todos = todos.concat({ text });
this.setState({ todos, text: '' });
};
render({ }, { todos, text }) {
return (
<form onSubmit={this.addTodo} action="javascript:">
<input value={text} onInput={this.setText} />
<button type="submit">Add</button>
<ul>
{ todos.map( todo => (
<li>{todo.text}</li>
)) }
</ul>
</form>
);
}
}

最佳答案

这些是 class instance fields (属性初始值设定项)。他们目前是第 2 阶段提案。

它们的使用(与 importexport 和其他 JS 引擎本身不支持的功能一起使用)意味着应该使用 Babel 来转换示例.

关于javascript - JS ES6 类定义 : Example on Preact homepage: I've never seen this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41799395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com