class Animal {
constructor(){
console.log(this);
this.type = 'animal'
}
says(say){
setTimeout(function(){
console.log(this);
console.log(this.type + ' says ' + say)
}, 1000)
}
}
为什么一个this是Animal对象,一个this是window对象?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这是JS中一个不好的地方:
setTimeout的执行上下文是window对象而不是class Animal. 导致了setTimeout中的函数是基于全局作用域执行, 其中的this指向了window可以这样解决(ES6):
setTimeout 里面 的匿名函数是作为函数调用的,而不是 对象的方法。。而作为函数调用的this 就是指向全局啊。。而不是setTimeout剥离你的上下文。。。里面就是个函数,不信你可以这么写。
一.
JS中函数内的上下文是由函数的调用者决定的,setTimeout的调用者是window,所以会log出window。这里如楼上所说,是可以使用箭头函数解决的,因为箭头函数的上下文是其所处环境决定的。
闭包,函数嵌套函数,可以访问函数中的局部变量