--- id: 56bbb991ad1ed5201cd392cf title: Write Reusable JavaScript with Functions challengeType: 1 videoUrl: '' localeTitle: 用函数编写可重用的JavaScript --- ## Description
在JavaScript中,我们可以将代码划分为称为函数的可重用部分。这是一个函数的例子:
function functionName(){
console.log(“Hello World”);
}
您可以使用其名称后跟括号来调用或调用此函数,如下所示: functionName();每次调用该函数时,它都会在开发控制台上打印出"Hello World"消息。每次调用函数时,都会执行大括号之间的所有代码。
## Instructions
  1. 创建一个名为reusableFunction的函数,它将"Hi World"打印到开发控制台。
  2. 调用该功能。
## Tests
```yml tests: - text: reusableFunction应该是一个函数 testString: 'assert(typeof reusableFunction === "function", "reusableFunction should be a function");' - text: reusableFunction应该将“Hi World”输出到开发控制台 testString: 'assert("Hi World" === logOutput, "reusableFunction should output "Hi World" to the dev console");' - text: 定义后调用reusableFunction testString: 'assert(/^\s*reusableFunction\(\)\s*;/m.test(code), "Call reusableFunction after you define it");' ```
## Challenge Seed
```js // Example function ourReusableFunction() { console.log("Heyya, World"); } ourReusableFunction(); // Only change code below this line ```
### Before Test
```js var logOutput = ""; var originalConsole = console function capture() { var nativeLog = console.log; console.log = function (message) { if(message && message.trim) logOutput = message.trim(); if(nativeLog.apply) { nativeLog.apply(originalConsole, arguments); } else { var nativeMsg = Array.prototype.slice.apply(arguments).join(' '); nativeLog(nativeMsg); } }; } function uncapture() { console.log = originalConsole.log; } capture(); ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```