搜尋

首頁  >  問答  >  主體

javascript - React事件中的 this為什麼是null?

為什麼this為null?

class App extends Component {


    handleClick(e) {
        console.log(this)//为什么是 null?
    }

    render() {
        return (
         
            <h2 onClick={this.handleClick}>React</h2>

        );
    }
}

export default App;

下面這種情況如何解釋?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button onclick="func()">click</button> // 打印 undefined
<button onclick="app.handleClick()">click</button> // 打印 App
<script>

    class App {
        handleClick() {
            console.log(this)
        }
    }
    const app = new App
    const func = app.handleClick
</script>
</body>
</html>

所以什麼時候會印出undefined,什麼時候會印null,什麼時候會正常列印?

習慣沉默習慣沉默2858 天前532

全部回覆(1)我來回復

  • PHPz

    PHPz2017-05-19 10:33:29

    雷雷

    回覆
    0
  • 取消回覆