1.普通函数 this指向window
function fn() {
console.log("普通函数的this" + this);
}
window.fn();
fn.call();
2.对象的方法 this指向的是对象 o
var o = {
sayHi: function () {
console.log("普通函数的this" + this);
},
};
o.sayHi();
3.构造函数 this 指向 ldh这个实例对象 原型对象里面的this 指向的也是ldh这个实例对象
function Star() {}
Star.prototype.sing = function () {
}
var ldh = new Star();
4.绑定事件函数 this 指向的是函数的调用者 btn这个按钮对象
var btn = document.querySelector("button");
btn.onclick = function () {
//点击了按钮就可以调用这个函数
console.log("普通函数的this" + this);
};
5.定时器函数 this指向的是window
setInterval(function () {
console.log("普通函数的this" + this);
}, 1000); //这个函数是定时器自动1秒钟调用一次
6. 立即执行函数 this指向的是window
(function () {
console.log("今天也要好好敲代码哦");
})();
// 立即执行函数是自动调用