Skip to content

Commit a72b96f

Browse files
committed
feat: externalize hoisting exercise
Keep the localized exercise definitions in one Markdown file per locale while preserving their exact placement in the article through stable references. Register the nested exercise source so translation drift can be tracked independently from the surrounding answer.
1 parent 68e91f9 commit a72b96f

8 files changed

Lines changed: 121 additions & 104 deletions

File tree

‎langnostic.config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export default {
1717
source: './questions/*/en-US.mdx',
1818
target: './questions/*/{locale}.mdx',
1919
},
20+
{
21+
source: './questions/*/exercises/en-US.mdx',
22+
target: './questions/*/exercises/{locale}.mdx',
23+
},
2024
],
2125
},
2226
],

‎questions/explain-hoisting/en-US.mdx‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -171,41 +171,7 @@ var expressed = function () {
171171
};
172172
```
173173

174-
<KnowledgeCheck id="function-declaration-vs-expression" type="single-select">
175-
<KnowledgeCheck.Prompt>
176-
What happens when this code runs?
177-
178-
```js
179-
console.log(declared());
180-
console.log(expressed());
181-
182-
function declared() {
183-
return 'function declaration';
184-
}
185-
186-
var expressed = function () {
187-
return 'function expression';
188-
};
189-
```
190-
191-
</KnowledgeCheck.Prompt>
192-
<KnowledgeCheck.Option value="both-log-values">
193-
Both strings are logged successfully.
194-
</KnowledgeCheck.Option>
195-
<KnowledgeCheck.Option value="declaration-then-type-error" correct>
196-
`'function declaration'` is logged, then calling `expressed` throws a
197-
`TypeError`.
198-
</KnowledgeCheck.Option>
199-
<KnowledgeCheck.Option value="reference-error-before-output">
200-
A `ReferenceError` is thrown before anything is logged.
201-
</KnowledgeCheck.Option>
202-
<KnowledgeCheck.Explanation>
203-
Function declarations are initialized with their function body before
204-
execution. The `var expressed` binding is also available, but its function
205-
assignment has not run yet, so its value is `undefined` and calling it
206-
throws a `TypeError`.
207-
</KnowledgeCheck.Explanation>
208-
</KnowledgeCheck>
174+
<Exercise id="function-declaration-vs-expression" />
209175

210176
- `declared()` returns `'function declaration'`. Function declarations are fully hoisted — both the identifier binding and the function body are available from the top of the scope.
211177
- `expressed()` throws `TypeError: expressed is not a function`. The `var expressed` binding is hoisted and initialized to `undefined`, but the assignment of the function expression happens at its source location. Calling `undefined()` produces the `TypeError`.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"frontmatter": {},
3+
"content": {
4+
"source": {
5+
"locale": "en-US",
6+
"hashes": [
7+
"1b07636e",
8+
"5f3262df",
9+
"b43e524d",
10+
"302b635a",
11+
"5657b6fa",
12+
"d6807ecb",
13+
"2deb5771",
14+
"da047d97"
15+
]
16+
},
17+
"targets": {
18+
"zh-CN": [
19+
"1b07636e",
20+
"5f3262df",
21+
"b43e524d",
22+
"302b635a",
23+
"5657b6fa",
24+
"d6807ecb",
25+
"2deb5771",
26+
"da047d97"
27+
]
28+
}
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## function-declaration-vs-expression [single-select]
2+
3+
### Prompt
4+
5+
What happens when this code runs?
6+
7+
```js
8+
console.log(declared());
9+
console.log(expressed());
10+
11+
function declared() {
12+
return 'function declaration';
13+
}
14+
15+
var expressed = function () {
16+
return 'function expression';
17+
};
18+
```
19+
20+
### Options
21+
22+
- [ ] `both-log-values`: Both strings are logged successfully.
23+
- [x] `declaration-then-type-error`: `'function declaration'` is logged, then calling `expressed` throws a `TypeError`.
24+
- [ ] `reference-error-before-output`: A `ReferenceError` is thrown before anything is logged.
25+
26+
### Explanation
27+
28+
Function declarations are initialized with their function body before execution. The `var expressed` binding is also available, but its function assignment has not run yet, so its value is `undefined` and calling it throws a `TypeError`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## function-declaration-vs-expression [single-select]
2+
3+
### Prompt
4+
5+
O que acontece quando este código é executado?
6+
7+
```js
8+
console.log(declared());
9+
console.log(expressed());
10+
11+
function declared() {
12+
return 'function declaration';
13+
}
14+
15+
var expressed = function () {
16+
return 'function expression';
17+
};
18+
```
19+
20+
### Options
21+
22+
- [ ] `both-log-values`: As duas strings são exibidas normalmente.
23+
- [x] `declaration-then-type-error`: `'function declaration'` é exibido e, em seguida, chamar `expressed` lança um `TypeError`.
24+
- [ ] `reference-error-before-output`: Um `ReferenceError` é lançado antes de qualquer saída.
25+
26+
### Explanation
27+
28+
Declarações de função são inicializadas com o corpo da função antes da execução. A associação `var expressed` também existe, mas a atribuição da função ainda não ocorreu. Portanto, seu valor é `undefined` e chamá-la lança um `TypeError`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## function-declaration-vs-expression [single-select]
2+
3+
### Prompt
4+
5+
运行以下代码时会发生什么?
6+
7+
```js
8+
console.log(declared());
9+
console.log(expressed());
10+
11+
function declared() {
12+
return 'function declaration';
13+
}
14+
15+
var expressed = function () {
16+
return 'function expression';
17+
};
18+
```
19+
20+
### Options
21+
22+
- [ ] `both-log-values`: 两个字符串都会正常输出。
23+
- [x] `declaration-then-type-error`: 先输出 `'function declaration'`,然后调用 `expressed` 时抛出 `TypeError`。
24+
- [ ] `reference-error-before-output`: 在输出任何内容之前抛出 `ReferenceError`。
25+
26+
### Explanation
27+
28+
执行代码前,函数声明已经用函数体完成初始化。`var expressed` 绑定也已存在,但函数赋值尚未执行,因此其值为 `undefined`,调用它会抛出 `TypeError`。

‎questions/explain-hoisting/pt-BR.mdx‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,7 @@ var bar = function () {
3232
console.log(bar); // [Function: bar]
3333
```
3434

35-
<KnowledgeCheck id="function-declaration-vs-expression" type="single-select">
36-
<KnowledgeCheck.Prompt>
37-
O que acontece quando este código é executado?
38-
39-
```js
40-
console.log(declared());
41-
console.log(expressed());
42-
43-
function declared() {
44-
return 'function declaration';
45-
}
46-
47-
var expressed = function () {
48-
return 'function expression';
49-
};
50-
```
51-
52-
</KnowledgeCheck.Prompt>
53-
<KnowledgeCheck.Option value="both-log-values">
54-
As duas strings são exibidas normalmente.
55-
</KnowledgeCheck.Option>
56-
<KnowledgeCheck.Option value="declaration-then-type-error" correct>
57-
`'function declaration'` é exibido e, em seguida, chamar `expressed` lança
58-
um `TypeError`.
59-
</KnowledgeCheck.Option>
60-
<KnowledgeCheck.Option value="reference-error-before-output">
61-
Um `ReferenceError` é lançado antes de qualquer saída.
62-
</KnowledgeCheck.Option>
63-
<KnowledgeCheck.Explanation>
64-
Declarações de função são inicializadas com o corpo da função antes da
65-
execução. A associação `var expressed` também existe, mas a atribuição da
66-
função ainda não ocorreu. Portanto, seu valor é `undefined` e chamá-la lança
67-
um `TypeError`.
68-
</KnowledgeCheck.Explanation>
69-
</KnowledgeCheck>
35+
<Exercise id="function-declaration-vs-expression" />
7036

7137
Variáveis declaradas via `let` e `const` também sofrem o hoisted. No entanto, ao contrário de `var` e `function`, eles não são inicializados e acessá-los antes que a declaração resulte em uma exceção `ReferenceError`. A variável está em uma "zona temporária morta" desde o início do bloco até que a declaração seja processada.
7238

‎questions/explain-hoisting/zh-CN.mdx‎

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -104,40 +104,7 @@ function foo() {
104104
}
105105
```
106106

107-
<KnowledgeCheck id="function-declaration-vs-expression" type="single-select">
108-
<KnowledgeCheck.Prompt>
109-
运行以下代码时会发生什么?
110-
111-
```js
112-
console.log(declared());
113-
console.log(expressed());
114-
115-
function declared() {
116-
return 'function declaration';
117-
}
118-
119-
var expressed = function () {
120-
return 'function expression';
121-
};
122-
```
123-
124-
</KnowledgeCheck.Prompt>
125-
<KnowledgeCheck.Option value="both-log-values">
126-
两个字符串都会正常输出。
127-
</KnowledgeCheck.Option>
128-
<KnowledgeCheck.Option value="declaration-then-type-error" correct>
129-
先输出 `'function declaration'`,然后调用 `expressed` 时抛出
130-
`TypeError`。
131-
</KnowledgeCheck.Option>
132-
<KnowledgeCheck.Option value="reference-error-before-output">
133-
在输出任何内容之前抛出 `ReferenceError`。
134-
</KnowledgeCheck.Option>
135-
<KnowledgeCheck.Explanation>
136-
执行代码前,函数声明已经用函数体完成初始化。`var expressed`
137-
绑定也已存在,但函数赋值尚未执行,因此其值为 `undefined`,调用它会抛出
138-
`TypeError`。
139-
</KnowledgeCheck.Explanation>
140-
</KnowledgeCheck>
107+
<Exercise id="function-declaration-vs-expression" />
141108

142109
这同样适用于生成器 (`function*`)、异步函数 (`async function`) 和异步函数生成器 (`async function*`)。
143110

0 commit comments

Comments
 (0)