为什么我可以访问和使用 event.currentTarget 但是当我打印事件时 currentTarget 为空?

How can it be that I can access and use event.currentTarget but when I print the event then currentTarget is null?

在 React 中考虑以下代码:

// rendered:
<input type="file" onChange={onFileSelected} />

// Event handler:
function onFileSelected(event: ChangeEvent<HTMLInputElement>) {
  console.log('event:');
  console.log(event);
  console.log('event.currentTarget.files:');
  console.log(event.currentTarget.files);
}

现在在我的 onFileSelected 函数上选择一个文件后被触发,这是输出: 我在屏幕截图中标记了我的困惑: event.currentTarget 为空,但 event.currentTarget.files 可访问?这怎么可能?

取自Event.currentTargetdocumentation

Note: The value of event.currentTarget is only available while the event is being handled. If you console.log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null. Instead, you can either directly console.log(event.currentTarget) to be able to view it in the console or use the debugger statement, which will pause the execution of your code thus showing you the value of event.currentTarget.