addEventListener的第二个参数为对象
- 改变this的指向
- 将对象的handleEvent方法作为事件回调。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="wrap">
handle-event
</div>
<script>
const div = document.querySelector("#wrap");
const obj = {
name: "huang",
handleEvent: function() {
console.log(this.name);
}
};
div.addEventListener("click", obj);
</script>
</body>
</html>