this指向
Tutorial: DOM操作
Category: JS
Published: 2026-04-07 13:58:26
Views: 20
Likes: 0
Comments: 0
this 指向问题
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<button>点击</button>
<script>
console.log(1, this);
function fn() {
console.log("fn: ", this);
}
window.fn();
window.setTimeout(function () {
console.log("setTimeout: ", this);
}, 1000);
var o = {
sayHi: function () {
console.log("o: ", this);
},
};
o.sayHi();
var btn = document.querySelector("button");
btn.addEventListener("click", function () {
console.log("btn: ", this);
});
function Fun() {
console.log("Fun: ", this);
}
var fun = new Fun();
</script>
</body>
</html>