JavaScript的几个概念摘录。

ES201X是JavaScript的一个版本。

ES2015新的feature

新的声明类型let, const,配合Block Scope。(if, forEach,)

之前:

var,  Global scope和function scope。

之后:

let, const , 这2个类型声明用在Block Scope内。

声明一次还是多次?

let, const只能声明一次。但var声明可以反复声明:

const num = 5;
const num = 7; // SyntaxError: identifier 'num' has already been declared

var x = 2; // x = 2
var x = 4; // x = 4

const 用于不会改变的值。

⚠️const x = {} , 可以改变其内的属性值,或增加新的key/value对儿。

这是因为,const variables当处理arrays 或objects时会改变,

技术上讲,不是再分配一个新的值给它,而是仅仅改变内部的元素。

const farm = [];
farm       = ['rice', 'beans', 'maize'] // TypeError: Assignment to constant variable

//但可以
farm.push('rice')

Hoisting

Declarations will be brought to the top of the execution of the scope!

⚠️ var x = "hello" 这种声明加分配assign的写法无法Hoisting!!

函数声明也可以Hoisting.


猜你喜欢

转载自www.cnblogs.com/chentianwei/p/10197813.html
今日推荐