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

25. 什么是Hoisting #42

Open
webVueBlog opened this issue Jun 6, 2022 · 0 comments
Open

25. 什么是Hoisting #42

webVueBlog opened this issue Jun 6, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

提升是一种 JavaScript 机制,其中变量、函数声明和类在代码执行之前被移动到其作用域的顶部。请记住,JavaScript 只提升声明,而不是初始化。我们举一个变量提升的简单例子,

console.log(message); //output : undefined
var message = "The variable Has been hoisted";

上面的代码对解释器来说如下所示,

var message;
console.log(message);
message = "The variable Has been hoisted";

以同样的方式,函数声明也被提升

message("Good morning"); //Good morning

function message(name) {
  console.log(name);
}

这种提升使函数在声明之前可以安全地在代码中使用。

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

No branches or pull requests

1 participant