We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
JS 中有3种声明方式:var,let 以及 const。
var
let
const
var 变量声明的作用域是当前的执行环境,函数或者全局,不具有块级作用域。
let 和 const 都具有块级作用域,前者用于变量,后者用于常量。
在全局作用域,var 声明的变量会成为 window 的一个属性,let 和 const 则不会。
window
configurable: false
configurable: true
var varname1 = 'varname1' varname2 = 'varname2' Object.getOwnPropertyDescriptor(window, 'varname1') Object.getOwnPropertyDescriptor(window, 'varname2')
The text was updated successfully, but these errors were encountered:
var、let、const 以及 function 声明无论发生在哪里,都会在执行代码之前被处理,也就是声明提升,声明被提升至当前作用域的顶部。
function
console.log(varname) // undefined var varname = 'someValue'
以上代码等价于
var varname console.log(varname) varname = 'someValue'
var 声明提升之后,varname 会被初始化为 undefined,所以 console.log(varname) 会打印出 undefined,直到代码执行至 varname 的赋值语句。
varname
undefined
console.log(varname)
暂存死区同样适用于 const。
Sorry, something went wrong.
https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-not-hoisted-in-es6# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
No branches or pull requests
1. 声明
JS 中有3种声明方式:
var
,let
以及const
。var
var
变量声明的作用域是当前的执行环境,函数或者全局,不具有块级作用域。let
和const
let
和const
都具有块级作用域,前者用于变量,后者用于常量。在全局作用域,
var
声明的变量会成为window
的一个属性,let
和const
则不会。已声明变量和未声明变量
window
的一个属性)。configurable: false
),未声明变量是可配置的(configurable: true
)。The text was updated successfully, but these errors were encountered: