Skip to content

Commit

Permalink
update es6.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi-love committed May 6, 2016
1 parent 193072d commit 3866685
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
40 changes: 20 additions & 20 deletions _posts/2016-04-29-JavaScript-es6.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,95 +17,95 @@ let 与 var 都是用来定义变量的。var 全局范围有效,let局部有效

`for`循环最合适 `let`定义变量:

{%highlight js%}
```js
var arr = [1,2];
for(let i = 0 ; i < arr.length ;i++){}
console.log(i);
//ReferenceError: i is not defined
{%endhighlight%}
```

`let``var` 作用域区别:
{%highlight js%}
```js
var arr = [];
for(var i = 0 ; i < 10 ; i++){
arr[i] = function(){
console.log(i)
}
}
arr[6]();//10
{%endhighlight%}
```
`i` 是全局的,所以都会输出最后i的值。

使用 `let`,声明的变量仅在块级作用域内有效,最后输出的是6。
{%highlight js%}
```js
var arr = [];
for(let i = 0 ; i < 10 ; i++){
arr[i] = function(){
console.log(i)
}
}
arr[6]();//6
{%endhighlight%}
```
每次循环 `i` 都是一个新的变量。

#### 不存在变量提升
`let` 不像 `var` 变量。可以先使用后定义。
{%highlight js%}
```js
console.log(a,b);//ReferenceError: b is not defined
var a = 10;
let b = 2;
{%endhighlight%}
```

#### 暂时性死区
只要块级作用域存在 `let`命令,它所声明的变量就会绑定在这个区域。
{%highlight js%}
```js
var a = 123;
if(true){
console.log(a);//ReferenceError: a is not defined
let a;
}
{%endhighlight%}
```

隐蔽的死区。
{%highlight js%}
```js
function add(x = y, y = 2) {
return x+y;
}
add(); // 报错
{%endhighlight%}
```

#### 不允许重复声明
let不允许在相同作用域内,重复声明同一个变量。
{%highlight js%}
```js
let a = 10;
let a = 1;
{%endhighlight%}
```

### const常量
const也用来声明变量,但是声明的是常量。一旦声明,常量的值就不能改变。

普通模式:
{%highlight js%}
```js
const c = 123;
c = 12;
console.log(c);//123
{%endhighlight%}
```

严格模式:
{%highlight js%}
```js
'use strict';
const c = 123;
c = 12;//TypeError: Assignment to constant variable.
console.log(c);
{%endhighlight%}
```

作用域:
{%highlight js%}
```js
if(true){
const a =12;
}
console.log(a);
{%endhighlight%}
```

const变量必须先申明后使用。

3 changes: 2 additions & 1 deletion _posts/2016-05-06-detect.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function platform(ua){
os.win = ua.match("Win");
os.mac = ua.match("Mac");
os.xll = ua.match("X11") || ua.match("Linux");
return os;

return os;
};
var os = platform();
```

0 comments on commit 3866685

Please sign in to comment.