Skip to content

Commit 7e67178

Browse files
authored
Merge pull request lydiahallie#215 from ConardLi/master
Translate the question 95 - 101 into Chinese
2 parents 9370751 + def44b3 commit 7e67178

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed

zh-CN/README-zh_CN.md

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,3 +2978,250 @@ getItems(["banana", "apple"], "pear", "orange")
29782978
上述例子是有效的,将会返回数组:`[ 'banana', 'apple', 'orange', 'pear' ]`
29792979
</p>
29802980
</details>
2981+
2982+
2983+
2984+
###### <a name=20190817></a>95. 输出什么?
2985+
2986+
```javascript
2987+
function nums(a, b) {
2988+
if
2989+
(a > b)
2990+
console.log('a is bigger')
2991+
else
2992+
console.log('b is bigger')
2993+
return
2994+
a + b
2995+
}
2996+
2997+
console.log(nums(4, 2))
2998+
console.log(nums(1, 2))
2999+
```
3000+
3001+
- A: `a is bigger`, `6` and `b is bigger`, `3`
3002+
- B: `a is bigger`, `undefined` and `b is bigger`, `undefined`
3003+
- C: `undefined` and `undefined`
3004+
- D: `SyntaxError`
3005+
3006+
<details><summary><b>答案</b></summary>
3007+
<p>
3008+
3009+
#### 答案: B
3010+
3011+
3012+
在JavaScript中,我们不必显式地编写分号(`;`),但是JavaScript引擎仍然在语句之后自动添加分号。这称为**自动分号插入**。例如,一个语句可以是变量,或者像`throw``return``break`这样的关键字。
3013+
3014+
在这里,我们在新的一行上写了一个`return`语句和另一个值`a + b `。然而,由于它是一个新行,引擎并不知道它实际上是我们想要返回的值。相反,它会在`return`后面自动添加分号。你可以这样看:
3015+
3016+
```javascript
3017+
return;
3018+
a + b
3019+
```
3020+
3021+
这意味着永远不会到达`a + b`,因为函数在`return`关键字之后停止运行。如果没有返回值,就像这里,函数返回`undefined`。注意,在`if/else`语句之后没有自动插入!
3022+
3023+
</p>
3024+
</details>
3025+
3026+
---
3027+
3028+
###### 96. 输出什么?
3029+
3030+
```javascript
3031+
class Person {
3032+
constructor() {
3033+
this.name = "Lydia"
3034+
}
3035+
}
3036+
3037+
Person = class AnotherPerson {
3038+
constructor() {
3039+
this.name = "Sarah"
3040+
}
3041+
}
3042+
3043+
const member = new Person()
3044+
console.log(member.name)
3045+
```
3046+
3047+
- A: `"Lydia"`
3048+
- B: `"Sarah"`
3049+
- C: `Error: cannot redeclare Person`
3050+
- D: `SyntaxError`
3051+
3052+
<details><summary><b>答案</b></summary>
3053+
<p>
3054+
3055+
#### 答案: B
3056+
3057+
3058+
我们可以将类设置为等于其他类/函数构造函数。 在这种情况下,我们将`Person`设置为`AnotherPerson`。 这个构造函数的名字是`Sarah`,所以新的`Person`实例`member`上的name属性是`Sarah`
3059+
3060+
3061+
</p>
3062+
</details>
3063+
3064+
---
3065+
3066+
###### 97. 输出什么?
3067+
3068+
```javascript
3069+
const info = {
3070+
[Symbol('a')]: 'b'
3071+
}
3072+
3073+
console.log(info)
3074+
console.log(Object.keys(info))
3075+
```
3076+
3077+
- A: `{Symbol('a'): 'b'}` and `["{Symbol('a')"]`
3078+
- B: `{}` and `[]`
3079+
- C: `{ a: "b" }` and `["a"]`
3080+
- D: `{Symbol('a'): 'b'}` and `[]`
3081+
3082+
<details><summary><b>答案</b></summary>
3083+
<p>
3084+
3085+
#### 答案: D
3086+
3087+
3088+
`Symbol`类型是不可枚举的。`Object.keys`方法返回对象上的所有可枚举的键属性。`Symbol`类型是不可见的,并返回一个空数组。 记录整个对象时,所有属性都是可见的,甚至是不可枚举的属性。
3089+
3090+
这是`Symbol`的众多特性之一:除了表示完全唯一的值(防止对象意外名称冲突,例如当使用2个想要向同一对象添加属性的库时),您还可以`隐藏`这种方式对象的属性(尽管不完全。你仍然可以使用`Object.getOwnPropertySymbols()`方法访问 `Symbol`
3091+
3092+
</p>
3093+
</details>
3094+
3095+
---
3096+
3097+
###### 98. 输出什么?
3098+
3099+
```javascript
3100+
const getList = ([x, ...y]) => [x, y]
3101+
const getUser = user => { name: user.name, age: user.age }
3102+
3103+
const list = [1, 2, 3, 4]
3104+
const user = { name: "Lydia", age: 21 }
3105+
3106+
console.log(getList(list))
3107+
console.log(getUser(user))
3108+
```
3109+
3110+
- A: `[1, [2, 3, 4]]` and `undefined`
3111+
- B: `[1, [2, 3, 4]]` and `{ name: "Lydia", age: 21 }`
3112+
- C: `[1, 2, 3, 4]` and `{ name: "Lydia", age: 21 }`
3113+
- D: `Error` and `{ name: "Lydia", age: 21 }`
3114+
3115+
<details><summary><b>答案</b></summary>
3116+
<p>
3117+
3118+
#### 答案: A
3119+
3120+
`getList`函数接收一个数组作为其参数。 在`getList`函数的括号之间,我们立即解构这个数组。 您可以将其视为:
3121+
3122+
`[x, ...y] = [1, 2, 3, 4]`
3123+
3124+
3125+
使用剩余的参数`... y`,我们将所有剩余参数放在一个数组中。 在这种情况下,其余的参数是`2``3``4``y`的值是一个数组,包含所有其余参数。 在这种情况下,`x`的值等于`1`,所以当我们打印`[x,y]`时,会打印`[1,[2,3,4]]`
3126+
3127+
`getUser`函数接收一个对象。对于箭头函数,如果只返回一个值,我们不必编写花括号。但是,如果您想从一个箭头函数返回一个对象,您必须在圆括号之间编写它,否则不会返回任何值!下面的函数将返回一个对象:
3128+
3129+
```const getUser = user => ({ name: user.name, age: user.age })```
3130+
3131+
由于在这种情况下不返回任何值,因此该函数返回`undefined`
3132+
3133+
</p>
3134+
</details>
3135+
3136+
---
3137+
3138+
###### 99. 输出什么?
3139+
3140+
```javascript
3141+
const name = "Lydia"
3142+
3143+
console.log(name())
3144+
```
3145+
3146+
- A: `SyntaxError`
3147+
- B: `ReferenceError`
3148+
- C: `TypeError`
3149+
- D: `undefined`
3150+
3151+
<details><summary><b>答案</b></summary>
3152+
<p>
3153+
3154+
#### 答案: C
3155+
3156+
3157+
变量`name`保存字符串的值,该字符串不是函数,因此无法调用。
3158+
3159+
当值不是预期类型时,会抛出`TypeErrors`。 JavaScript期望`name`是一个函数,因为我们试图调用它。 但它是一个字符串,因此抛出`TypeError``name is not a function`
3160+
3161+
当你编写了一些非有效的JavaScript时,会抛出语法错误,例如当你把`return`这个词写成`retrun`时。
3162+
当JavaScript无法找到您尝试访问的值的引用时,抛出`ReferenceErrors`
3163+
3164+
</p>
3165+
</details>
3166+
3167+
---
3168+
3169+
###### 100. 输出什么?
3170+
3171+
```javascript
3172+
// 🎉✨ This is my 100th question! ✨🎉
3173+
3174+
const output = `${[] && 'Im'}possible!
3175+
You should${'' && `n't`} see a therapist after so much JavaScript lol`
3176+
```
3177+
3178+
- A: `possible! You should see a therapist after so much JavaScript lol`
3179+
- B: `Impossible! You should see a therapist after so much JavaScript lol`
3180+
- C: `possible! You shouldn't see a therapist after so much JavaScript lol`
3181+
- D: `Impossible! You shouldn't see a therapist after so much JavaScript lol`
3182+
3183+
<details><summary><b>答案</b></summary>
3184+
<p>
3185+
3186+
#### 答案: B
3187+
3188+
3189+
`[]`是一个真值。 使用`&&`运算符,如果左侧值是真值,则返回右侧值。 在这种情况下,左侧值`[]`是一个真值,所以返回`Im`
3190+
3191+
`""`是一个假值。 如果左侧值是假的,则不返回任何内容。 `n't`不会被退回。
3192+
3193+
</p>
3194+
</details>
3195+
3196+
---
3197+
3198+
###### 101.输出什么?
3199+
3200+
```javascript
3201+
const one = (false || {} || null)
3202+
const two = (null || false || "")
3203+
const three = ([] || 0 || true)
3204+
3205+
console.log(one, two, three)
3206+
```
3207+
3208+
- A: `false` `null` `[]`
3209+
- B: `null` `""` `true`
3210+
- C: `{}` `""` `[]`
3211+
- D: `null` `null` `true`
3212+
3213+
<details><summary><b>答案</b></summary>
3214+
<p>
3215+
3216+
#### 答案: C
3217+
3218+
使用`||`运算符,我们可以返回第一个真值。 如果所有值都是假值,则返回最后一个值。
3219+
3220+
`(false || {} || null)`:空对象`{}`是一个真值。 这是第一个(也是唯一的)真值,它将被返回。`one`等于`{}`
3221+
3222+
`(null || false ||“”)`:所有值都是假值。 这意味着返回传递的值`""``two`等于`""`
3223+
3224+
`([] || 0 ||“”)`:空数组`[]`是一个真值。 这是第一个返回的真值。 `three`等于`[]`
3225+
3226+
</p>
3227+
</details>

0 commit comments

Comments
 (0)