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

第 108 题:请写出如下代码的打印结果 #34

Open
DreamLee1997 opened this issue Oct 6, 2019 · 0 comments
Open

第 108 题:请写出如下代码的打印结果 #34

DreamLee1997 opened this issue Oct 6, 2019 · 0 comments

Comments

@DreamLee1997
Copy link
Owner

var name = 'Tom';
(function() {
    if (typeof name == 'undefined') {
        var name = 'Jack';
        console.log('Goodbye ' + name);
    } else {
        console.log('Hello ' + name);
    }
})();

解答:
Goodbye Jack
var 声明的变量没有块作用域,变量会提升到最近的 function 作用域的上层,但此时只是声明了变量,并没有赋值,到实际运行了赋值语句之后才有值,在之前值为 undefined

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