Who does this point to in this1 event binding
Traditional binding 1 Event binding behind the label
- html code:
js code<button id="btn" onclick="test()">按钮</button>
Click the button to print the result as a window objectfunction test() { console.log(this) }
- html code
js code<button id="btn" onclick="test(this)">按钮</button>
After clicking the button, the print result is the button dom nodefunction test() { console.log(this) }
Other traditional binding
//1. 有名函数绑定
document.getElementById('btn').onclick=test
function test() {
console.log(this)
}
//2.匿名函数绑定
document.getElementById('btn').onclick=function() {
console.log(this)//或者 test()
}
After clicking the button, the print result is the button dom node
Modern binding
//1. 有名函数绑定
document.getElementById('btn').addEventListener('click',test,false)
function test() {
console.log(this)
}
//2.匿名函数绑定
document.getElementById('btn').addEventListener('click',function(){
console.log(this)//或者 test()
},false)
After clicking the button, the print result is the button dom node