函数
Note: Functions created with the Function constructor do not create closures to their creation contexts; they always run in the window context (unless the function body starts with a “use strict”; statement, in which case the context is undefined).——MDN
function永远是在window的上下文中执行。
举个例子:
console.log(this)//Window function Super() { console.log(this); } Super()//Window function Super() { console.log(this); function Sub() { console.log(this); } } Super()//Window;Window
超时调用
超时调用的代码都是在全局作用域中执行的,因此函数中的this的值在非严格模式下指向window对象,在严格模式下是undefined——《Javascript高级程序设计》
举个例子:
function Super(){var timeoutId = setTimeout(function() {console.log(this)}, 1000)} Super()//Window