1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-29 22:12:21 +00:00

Fix some code syntax problem in docs/let.md

Followed the previous coding style, added some `;` and space to the code.
This commit is contained in:
Xcat Liu 2016-05-13 16:17:59 +08:00
parent 7f66176c1f
commit 78112e85b0

View File

@ -23,7 +23,7 @@ b // 1
```javascript
for (let i = 0; i < arr.length; i++) {}
console.log(i)
console.log(i);
//ReferenceError: i is not defined
```
@ -196,7 +196,7 @@ function f(){
}
}
f() // undefined
f(); // undefined
```
上面代码中函数f执行后输出结果为`undefined`原因在于变量提升导致内层的tmp变量覆盖了外层的tmp变量。
@ -251,7 +251,7 @@ ES6允许块级作用域的任意嵌套。
```javascript
{{{{
let insane = 'Hello World';
{let insane = 'Hello World';}
{let insane = 'Hello World'}
}}}};
```
@ -294,7 +294,7 @@ function f() { console.log('I am outside!'); }
return a;
}
}
f() // 报错
f(); // 报错
```
上面代码中,块级作用域外部,无法调用块级作用域内部定义的函数。如果确实需要调用,就要像下面这样处理。
@ -305,9 +305,9 @@ let f;
let a = 'secret';
f = function () {
return a;
};
}
}
f() // "secret"
f(); // "secret"
```
ES5的严格模式规定函数只能在顶层作用域和函数内声明其他情况比如`if`代码块、循环代码块)的声明都会报错。
@ -342,7 +342,7 @@ if (true)
if (true) {
function f() {}
}
f() // ReferenceError: f is not defined
f(); // ReferenceError: f is not defined
```
上面代码中,函数`f`是在块级作用域内部声明的,外部是不可用的。
@ -425,7 +425,7 @@ foo.prop = 123;
foo.prop
// 123
foo = {} // TypeError: "foo" is read-only
foo = {}; // TypeError: "foo" is read-only
```
上面代码中,常量`foo`储存的是一个地址,这个地址指向一个对象。不可变的只是这个地址,即不能把`foo`指向另一个地址,但对象本身是可变的,所以依然可以为其添加新属性。