hym1224 / js

0 stars 0 forks source link

Transfer parameter strategy #10

Open hym1224 opened 6 years ago

hym1224 commented 6 years ago

from https://juejin.im/post/5a5cb80151882573385fb84d

惰性求值

let x = 99;
function foo(p = x + 1) {
  console.log(p);
}

foo() // 100

x = 100;
foo() // 101

// 上面代码中,参数p的默认值是x + 1。这时,每次调用函数foo,都会重新计算x + 1,而不是默认p等于 100。