Javascript 神秘语法onClick={::this.submit}
这个周末,我在搜刮一些最近的react Repo,我遇到了一个使用ES6类语法进行组件合成的示例,它有点像这样Javascript 神秘语法onClick={::this.submit},javascript,reactjs,Javascript,Reactjs,这个周末,我在搜刮一些最近的react Repo,我遇到了一个使用ES6类语法进行组件合成的示例,它有点像这样 class MyThing extends Component { constructor(props) { super(props) this.state = {something: 'the thing'} } submit() { // do stuff } r
class MyThing extends Component {
constructor(props) {
super(props)
this.state = {something: 'the thing'}
}
submit() {
// do stuff
}
render() {
<div>
<button onClick={::this.submit}>Fire Submit</button>
</div>
}
}
类神话扩展组件{
建造师(道具){
超级(道具)
this.state={something:'thing'}
}
提交(){
//做事
}
render(){
开火投降
}
}
注意::this.submit
代替this.submit.bind(this)
它可以工作,我在任何地方都找不到关于这个特性的文档,我觉得自己像个疯子,这是什么
onClick={::this.dosomethinginsiderrenderwithoutdotbind}
语法调用,我在哪里可以读到更多关于它的信息 双冒号很详细,目前是一个ES7提案,因此它还没有成为定案,对此仍有很多争论。它也不允许传递参数。因此,如果你有这种需要,它的用途是有限的
还有一个“fat arrow”函数选项(用于实际函数而不是对它的调用),它在词汇上绑定到此函数
// Basic syntax:
(param1, param2, paramN) => { statements }
(param1, param2, paramN) => expression
// equivalent to: => { return expression; }
// Parentheses are optional when there's only one argument:
(singleParam) => { statements }
singleParam => { statements }
// A function with no arguments requires parentheses:
() => { statements }
// Advanced:
// Parenthesize the body to return an object literal expression:
params => ({foo: bar})
// Rest parameters are supported
(param1, param2, ...rest) => { statements }
@昆汀,就是这样!你很好,非常感谢。我接受这个答案,在你空闲的时候。请记住,这是一个0级功能,这意味着它离标准化还有很长的路要走。很有趣,但最好在自己的代码中避免。