Skip to content

Commit

Permalink
Merge pull request #60 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update function
  • Loading branch information
GuoXiCheng authored May 13, 2024
2 parents 9b9d76f + f8e9724 commit c882525
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/.vitepress/sidebar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
link: /javascript/ecma-script/generator
- text: JSON
link: /javascript/ecma-script/json
- text: 函数
items:
- text: 定义函数
link: /javascript/ecma-script/function/define-function
- text: BOM
base: /javascript/bom
items:
Expand Down
2 changes: 2 additions & 0 deletions src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import MarkMap from './MarkMap.vue';
- [迭代器](javascript/ecma-script/iterator)
- [生成器](javascript/ecma-script/generator)
- [JSON](javascript/ecma-script/json)
- 函数
- [定义函数](javascript/ecma-script/function/define-function)
- BOM
- [screen](javascript/bom/screen)
- 前端
Expand Down
65 changes: 65 additions & 0 deletions src/javascript/ecma-script/function/define-function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 定义函数

## 函数声明

函数声明会在执行前被提升到作用域的顶部,因此可以在声明之前调用函数。

```js
function functionName(arg1, arg2, ...argN) {
// 函数体
}
```

## 函数表达式

函数表达式通常被赋值给一个变量,这种方式的函数不会被提升,因此无法在声明之前调用函数。

```js
const functionName = function (arg1, arg2, ...argN) {
// 函数体
};
```

## 箭头函数

箭头函数是 ES6 新增的函数语法,它的特点是没有自己的 `this``arguments``super``new.target`,这些值由外围最近一层非箭头函数决定。

```js
const functionName = (arg1, arg2, ...argN) => {
// 函数体
};
```

## 即时调用函数

即时调用函数是在声明后立即调用的函数。

```js
(function () {
// 函数体
})();
```

## Generator 函数

Generator 函数是 ES6 新增的函数语法,它可以通过 `yield` 关键字暂停和恢复函数的执行。

```js
function* functionName() {
let id = 0;
while (true) {
yield id++;
}
}
```

## 异步函数

异步函数是 ES8 新增的函数语法,它可以通过 `await` 关键字等待异步操作的结果。

```js
async function functionName() {
const data = await fetch("https://api.example.com");
return data.json();
}
```

0 comments on commit c882525

Please sign in to comment.