49 lines
1.3 KiB
Markdown
49 lines
1.3 KiB
Markdown
---
|
||
id: 56533eb9ac21ba0edf2244be
|
||
title: 全局作用域和函数
|
||
challengeType: 1
|
||
videoUrl: 'https://scrimba.com/c/cQM7mCN'
|
||
forumTopicId: 18193
|
||
---
|
||
|
||
# --description--
|
||
|
||
在 JavaScript 中,<dfn>作用域</dfn>涉及到变量的作用范围。在函数外定义的变量具有 <dfn>全局</dfn> 作用域。这意味着,具有全局作用域的变量可以在代码的任何地方被调用。
|
||
|
||
这些没有使用`var`关键字定义的变量,会被自动创建在全局作用域中,形成全局变量。当在代码其他地方无意间定义了一个变量,刚好变量名与全局变量相同,这时会产生意想不到的后果。因此你应该总是使用var关键字来声明你的变量。
|
||
|
||
# --instructions--
|
||
|
||
在函数外声明一个`全局`变量`myGlobal`,并给它一个初始值`10`
|
||
|
||
在函数`fun1`的内部,**不**使用`var`关键字来声明`oopsGlobal`,并赋值为`5`。
|
||
|
||
# --hints--
|
||
|
||
应定义`myGlobal`。
|
||
|
||
```js
|
||
assert(typeof myGlobal != 'undefined');
|
||
```
|
||
|
||
`myGlobal`的值应为`10`。
|
||
|
||
```js
|
||
assert(myGlobal === 10);
|
||
```
|
||
|
||
应使用`var`关键字定义`myGlobal`。
|
||
|
||
```js
|
||
assert(/var\s+myGlobal/.test(code));
|
||
```
|
||
|
||
`oopsGlobal`应为全局变量且值为`5`。
|
||
|
||
```js
|
||
assert(typeof oopsGlobal != 'undefined' && oopsGlobal === 5);
|
||
```
|
||
|
||
# --solutions--
|
||
|