副作用
- 修改任何外部的变量、对象属性、数据结构
- 控制台输入输出交互
- 文件操作,网络操作
- 抛出异常,错误终止
let score = [1, 2, 3]
// 对外部数据修改了,多人合作公用变量可能出问题
function getMaxScore() {
return score.sort((a, b) => a - b)[0]
}
纯函数(尽量写)
输入相同的值,每次都返回相同的结果
非纯函数
- 不利于单元测试
- 别人使用会有担忧 ```javascript let user = ‘ming’
function getString(user) {
let randomNum = Math.random()
return hello ${user}, you get ${randomNum}
}
非纯函数,random 会变 ```