this 直接写在全局中, this永远指向window
console.log(this);
全局中 定义函数,函数中的this永远指向window
function() {
console.log(this);
}
对象的属性值 等于 this ,this指向window
obj = {a: this}
自定义对象方法中的this, this指向 自定义对象
obj = {
fn : function(){ return this }
}
自定义对象方法中 返回 函数体,函数体中有this, this指向window
obj = {
fn: function() {
var that = this; // 自定义对象
return function() {
return [this, that]
}
}
}
构造函数写法, 没有加new , this 指向于window
function Dog(n) {
this.name = n
}
var d1 = Dog("a"); // 没有加new
构造函数写法, 加new, this指向于 自定义对象
function Dog(n) {
this.name = n
}
var d1 = new Dog("a");
局部作用域中声明的变量,不能通过this找到,因此this指向window(全局作用域)
function fn(){
var a = 1;
console.log(this.a); // undefined
}