- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- Dmitry Sheiko
- 176字
- 2021-07-15 17:36:26
Scoping
In the old days, we used to always go with the var statement for variable declarations. ES2015 introduces two new declaration variables--let and const. The var statement declares a variable in a function scope:
(function(){
var foo = 1;
if ( true ) {
var foo = 2;
console.log( foo );
}
console.log( foo );
}());
$ node es6.js
2
2
A variable declared with var (foo) spans the entire function scope, meaning that every time we reference it by name, we target the same variable. Both let and const operate on block scopes (if statement, for/while loops, and so on) as shown:
(function(){
let foo = 1;
if ( true ) {
let foo = 2;
console.log( foo );
}
console.log( foo );
}());
$ node es6.js
2
1
As you can see from the preceding example, we can declare a new variable in a block and it will exist only within that block. The statement const works the same, except it defines a constant that cannot be reassigned after it was declared.
推薦閱讀
- FreeSWITCH 1.8
- Magento 2 Theme Design(Second Edition)
- React.js Essentials
- 深度學習:算法入門與Keras編程實踐
- Java編程技術與項目實戰(zhàn)(第2版)
- Java系統(tǒng)化項目開發(fā)教程
- C#開發(fā)案例精粹
- Extreme C
- Flink技術內(nèi)幕:架構設計與實現(xiàn)原理
- Scratch從入門到精通
- 算法秘籍
- Zend Framework 2 Cookbook
- Visual FoxPro程序設計實驗教程
- Mastering Data Analysis with R
- 面向物聯(lián)網(wǎng)的Android應用開發(fā)與實踐