xxx => xx = xxx 这是什么写法 es6

node => input = node
es6箭头函数倒是经常用,搞不清为啥最后还来个赋值

render() {
    let input = '';
    return (
      <div>
        <input ref={node => input = node} />
        <button type="submit" onClick={() => {
          console.log(input.value);
        }}>添加</button>
      </div>
    );
  }
阅读 3.7k
4 个回答

简洁写法,相当于 xxx => {let xx = xxx; return xx;}

赋值就是赋值啊,没有特殊。

let input = '';
...
node => input = node

等价于

let input = '';
...
function fn(node) {
    input = node;
    return input;
}

就是箭头函数啊,只不过函数的主体就是一条 赋值语句,省略了函数大括号而已。

let foo;
console.log(foo=1);
let foo
function bar(){
    return foo=1;
}
let foo
()=>foo=1;

看明白没?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题