this的使用

129 阅读1分钟

全局 this 一般指向全局对象,浏览器中的全局对象就是 window

console.log(this.document === document); 
console.log(this === window); 
this.a = 91;
console.log(window.a); 

作为对象方法的函数的 this

var o = {
    prop: 37,
    f: function() {
        return this.prop;
    }
};
console.log(o.f());