赞
踩
this = window // 这个就不做过多说明
window.color = 'red'
let o = { color: blue}
function saycolor() {
consloe.log(this.color)
}
saycolor() // red this = window
o.saycolor = saycolor
o.saycolor() // blue this = o
window.color = 'red'
let o = { color: blue}
let saycolor = () => {
consloe.log(this.color)
}
saycolor() // red this = window
o.saycolor = saycolor
o.saycolor() // red this = window
var name = 'my name is window';
var obj ={
name:'my name is obj',
fn:function(){
var timer = null;
clearInterval(timer);
timer = setInterval(function(){
console.log(this.name)//my name is window
},1000)
}
}
window.identity = 'the window' let object = { identity = 'the object' getdentityFunc() { return function() { return this.identity } } } console.log(object.getdentityFunc()()) //the window // 把this保存到闭包可以访问到的变量上去。 window.identity = 'the window' let object = { identity = 'the object' getdentityFunc() { let that = this return function() { return that.identity } } } console.log(object.getdentityFunc()()) //the object
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。