freeCodeCamp/guide/chinese/javascript/tutorials/write-reusable-javascript-w.../index.md

33 lines
800 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Write Reusable JavaScript with Functions
localeTitle: 用函数编写可重用的JavaScript
---
在JavaScript中我们可以将代码划分为称为函数的可重用部分。
这是一个函数的例子:
```
function functionName() {
console.log("Hello World");
}
```
您可以使用其名称后跟括号来`call`或`invoke`此函数,如下所示:
```
functionName();
```
每次调用该函数时它都会在开发控制台上打印出“Hello World”消息。每次调用函数时都会执行大括号之间的所有代码。
这是另一个例子:
```
function otherFunctionName (a, b) {
return(a + b);
}
```
我们可以像这样`call`或`invoke`我们的函数:
```
otherFunctionName(1,2);
```
它将运行并将其返回值返回给我们。