From db97b3393192cd20a8e905107e1a4e15bba11394 Mon Sep 17 00:00:00 2001 From: Ruan Yifeng Date: Wed, 4 Mar 2015 18:51:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9let?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/function.md | 13 +++++++++++++ docs/let.md | 5 +++-- docs/style.md | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/function.md b/docs/function.md index 12f523e..cde5fb3 100644 --- a/docs/function.md +++ b/docs/function.md @@ -167,6 +167,19 @@ foo(2) // 2 上面代码中,参数y的默认值等于x,由于处在函数作用域,所以x等于参数x,而不是全局变量x。 +参数变量是默认声明的,所以不能用let或const再次声明。 + +```javascript + +function foo(x = 5) { + let x = 1; // error + const x = 2; // error +} + +``` + +上面代码中,参数变量x是默认声明的,在函数体中,不能用let或const再次声明,否则会报错。 + 参数默认值可以与解构赋值,联合起来使用。 ```javascript diff --git a/docs/let.md b/docs/let.md index 1cb8117..218c0b3 100644 --- a/docs/let.md +++ b/docs/let.md @@ -29,7 +29,6 @@ for(let i = 0; i < arr.length; i++){} console.log(i) //ReferenceError: i is not defined - ``` 上面代码的计数器i,只在for循环体内有效。 @@ -88,7 +87,7 @@ if (1) { ``` -上面代码中,由于块级作用域内typeof运行时,x还没有声明,所以会抛出一个ReferenceError。 +上面代码中,由于块级作用域内typeof运行时,x还没有值,所以会抛出一个ReferenceError。 只要块级作用域内存在let命令,它所声明的变量就“绑定”(binding)这个区域,不再受外部的影响。 @@ -105,6 +104,8 @@ if (true) { 上面代码中,存在全局量tmp,但是块级作用域内let又声明了一个局部变量tmp,导致后者绑定这个块级作用域,所以在let声明变量前,对tmp赋值会报错。 +ES6明确规定,如果区块中存在let和const命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。凡是在声明之前就使用这些命令,就会报错。 + 总之,在代码块内,使用let命令声明变量之前,该变量都是不可用的。这在语法上,称为“暂时性死区”(temporal dead zone,简称TDZ)。 ```javascript diff --git a/docs/style.md b/docs/style.md index fc631dd..6f7aeba 100644 --- a/docs/style.md +++ b/docs/style.md @@ -221,7 +221,7 @@ var React = require('react'); var Breadcrumbs = React.createClass({ render() { - return <nav />; + return