js函数中this的指向
函数的调用方式决定了 this 指向的不同:
| 调用方式 | 非严格模式 | 备注 |
|---|---|---|
| 普通函数调用 | window | 严格模式下是 undefined |
| 实例对象 | 原型方法中 this 也是实例对象 | |
| 对象方法调用 | 该方法所属对象 | 紧挨着的对象 |
| 事件绑定方法 | 绑定事件对象 | |
| 定时器函数 | window |
/*
*
* 函数中的this的指向
*
*
* 普通函数中的this是谁?-----window
* 对象.方法中的this是谁?----实例对象
* 定时器方法中的this是谁?----window
* 构造函数中的this是谁?-----实例对象
* 原型对象方法中的this是谁?---实例对象
*
*
* */
//严格模式:
// "use strict";//严格模式
//function f1() {
// console.log(this);//window
// }
//f1();
//普通函数
function f1() {
console.log(this); //window
}
f1();
//定时器中的this
setInterval(function () {
console.log(this); //window
},1000);
//构造函数
function Person() {
console.log(this); // 实例对象
//对象的方法
this.sayHi=function () {
console.log(this); // 实例对象
};
}
//原型中的方法
Person.prototype.eat=function () {
console.log(this); // 实例对象
};
var per=new Person();
console.log(per);
per.sayHi();
per.eat();
保存redis相关笔记

浙公网安备 33010602011771号