Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

执行上下文中的2道题 #17

Open
Clearives opened this issue Aug 30, 2019 · 1 comment
Open

执行上下文中的2道题 #17

Clearives opened this issue Aug 30, 2019 · 1 comment

Comments

@Clearives
Copy link
Owner

第1题

var scope = "global scope";
function checkscope(){
    var scope = "local scope";
    function f(){
        return scope;
    }
    return f;
}
checkscope()();

这端代码输出的答案是:local scope;为什么呢?
执行过程伪代码:

- [全局]EC
 - [local scope]EC
  - [f]EC
f函数执行能访问到local scope,所以输出local scope

第2题

var scope = "global scope";
function checkscope(){
    var scope = "local scope";
    function f(){
        return scope;
    }
    return f;
}
checkscope()();

这端代码输出的答案是:local scope;为什么呢?
执行过程伪代码:

- [全局]EC
 - [local scope]EC
  - 闭包
执行到闭包时,[local scope]EC已出栈,所以闭包内this指向window,但是我们知道闭包能访问上层变量,也就是[local scope]EC里的变量,所以打印出来的也是local scope
@Clearives Clearives changed the title 执行上下文中的一道题 执行上下文中的2道题 Aug 30, 2019
@Clearives
Copy link
Owner Author

函数声明的一个小问题

// f1
console.log(func1)  // Uncaught ReferenceError: func1 is not defined

func1 = function() {
 console.log(1)
}
// f2
console.log(func2)  // undefined

var func2 = function() {
 console.log(1)
}
// f3
console.log(func3)  // func3函数

function func3() {
 console.log(1)
}

参考这篇文章的时候想起的小问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant