在看draft-js给的例子,出现了困惑。
平时传参我都是直接
xxx={(ev, arg1, arg2,……) => {this.xxx(ev, arg1, arg2,……)}
官方给出的快速开始例子
class MyEditor extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.onChange = (editorState) => this.setState({editorState});
}
render() {
return (
);
}
}
想知道editorState参数是怎么传给onChange这个函数的?
我试了
this.onChange = (editorState) => {
var length = arguments.length;
console.log('change');
for (var i = 0; i < length; i++) {
if (arguments[i] == editorState) {
console.log(i);
}
}
this.setState({editorState})
};
arguments中并没有editorState参数。而如果直接输出是有的
this.onChange = (editorState) => {
console.log(editorState);
this.setState({editorState})
};
为什么呢?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这是Editor组件的一段源码,是这个组件传回来了你所要的参数。
你平时的写法是在标签里的写法,即{}内用js语法来解释
快速例子里的才是正常的写法
=>符号构建的函数的arguments跟function构建的函数的arguments是不一样的,你可以直接输出arguments看是什么东西
自己总结了下。
把theone1006的函数改造下
可以发现baz的arguments就是foo的arguments。
如果把baz函数单独提出
是会提示
arguments is not defined的。而后我试了
看出handleClick的arguments就是constructor的arguments。参数a, b, c和arguments不一致。
最后根据chhu1的答案,知道了参数是从哪来的。