副作用

  1. 修改任何外部的变量、对象属性、数据结构
  2. 控制台输入输出交互
  3. 文件操作,网络操作
  4. 抛出异常,错误终止
    1. let score = [1, 2, 3]
    2. // 对外部数据修改了,多人合作公用变量可能出问题
    3. function getMaxScore() {
    4. return score.sort((a, b) => a - b)[0]
    5. }

    纯函数(尽量写)

    输入相同的值,每次都返回相同的结果

非纯函数

  1. 不利于单元测试
  2. 别人使用会有担忧 ```javascript let user = ‘ming’

function getString(user) { let randomNum = Math.random() return hello ${user}, you get ${randomNum} }

非纯函数,random 会变 ```