import React, { Component } from 'react'
export default class kind extends Component {
fn(){
console.log(this)
}
render() {
return (
<div>
<button onClick={this.fn}>点击</button>
</div>
)
}
}
<button>点击</button>
</body>
<script type="text/javascript" src="./vue1.js"></script>
<script type="text/javascript">
function fn(a){
console.log(this)
// console.log(a)
}
var obj={
a:this.fn,
fn(){
console.log(this)
}
}
document.getElementsByTagName("button")[0].onclick=obj.fn
react类组件中绑定事件处理函数时相当于js中将函数直接作为事件处理函数一样,这时事件应该指向事件源,由于react-cli自身的严格模式导致this指向undefined,