Skip to content

Commit 05b852a

Browse files
committed
docs(types): fix object wangdoc#88
1 parent 34a476e commit 05b852a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

docs/types/object.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,19 @@ y // 1
171171

172172
JavaScript 引擎读到上面这行代码,会发现可能有两种含义。第一种可能是,这是一个表达式,表示一个包含`foo`属性的对象;第二种可能是,这是一个语句,表示一个代码区块,里面有一个标签`foo`,指向表达式`123`
173173

174-
为了避免这种歧义,V8 引擎规定,如果行首是大括号,一律解释为对象。不过,为了避免歧义,最好在大括号前加上圆括号
174+
为了避免这种歧义,JavaScript 引擎的做法是,如果遇到这种情况,无法确定是对象还是代码块,一律解释为代码块
175175

176176
```javascript
177-
({ foo: 123})
177+
{ console.log(123) } // 123
178+
```
179+
180+
上面的语句是一个代码块,而且只有解释为代码块,才能执行。
181+
182+
如果要解释为对象,最好在大括号前加上圆括号。因为圆括号的里面,只能是表达式,所以确保大括号只能解释为对象。
183+
184+
```javascript
185+
({ foo: 123 }) // 正确
186+
({ console.log(123) }) // 报错
178187
```
179188

180189
这种差异在`eval`语句(作用是对字符串求值)中反映得最明显。

0 commit comments

Comments
 (0)