Document注意
即使“使用严格”指令,此行为也将相同 之所以不存在,是因为类主体的语法范围内的代码 始终以严格模式执行。
但是当我尝试进行反应时,情况似乎与我上面引用的内容有所不同:
// 'use strict';
class LoggingButton extends React.Component {
handleClick = (e) => {
var methodOnly = LoggingButton.staticFunction;
methodOnly();
}
static staticFunction() {
console.log(this);
};
render() {
return (
<button onClick={this.handleClick}>
Click me
</button>
)
}
}
ReactDOM.render(
<LoggingButton />, document.getElementById('root')
);
您可以看到,我没有将此值传递给变量methodOnly。
因此,根据严格模式,在调用staticFunction时应为undefined
。但是它将改为打印窗口。
如果取消注释顶部的“ use strict”并再次运行代码,则单击按钮后,它将打印未定义的内容。
我不确定这是故意的还是由移植引起的。 因此,我在这里保留了如何编译代码的方式: npx babel --watch src --out-dir
dist --presets react-app/prod --source-maps --plugins transform-class-properties
如果是故意的,请让我知道为什么? 如果我看错文档,请更正我。 谢谢!