2021-07-29 11:08:52 +08:00
|
|
|
|
---
|
|
|
|
|
publish: false
|
|
|
|
|
---
|
|
|
|
|
|
2018-05-02 21:31:28 +08:00
|
|
|
|
|
|
|
|
|
### `callback && callback()`的含义
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
callback && callback()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
含义是:如果callback存在,则执行callback()函数。
|
|
|
|
|
|
|
|
|
|
这个 callback 通常作为函数的参数使用。举例:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
function foo(callback) {
|
|
|
|
|
{
|
2021-07-29 11:08:52 +08:00
|
|
|
|
// do something
|
2018-05-02 21:31:28 +08:00
|
|
|
|
}
|
2021-07-29 11:08:52 +08:00
|
|
|
|
callback && callback() // 不传 callback 参数,则不会执行 callback() 函数
|
2018-05-02 21:31:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 11:08:52 +08:00
|
|
|
|
foo(); // 只执行do something中的代码
|
|
|
|
|
foo(callback);//callback是另一个函数,将此函数传入 foo,将会执行callback()
|
2018-05-02 21:31:28 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|