就像 Redux 推荐的那样,不要在 reducer
里 mutate(修改) state,但是修改 reference (改变一个变量的指向)是可以的,这也叫做 reference assignment
,我并不知道中文怎么称呼,大概叫引用赋值。
function main(options) {
// 很多时候你要给函数参数指定默认值
// 那么向下面这样直接让这个变量指向另一个对象,也就是 `reference assignment` 是可以的
// 这里的 options 只局限于这个 local function 中,它只是指向了另一个变量
// 作用域之外的 options 并没有被修改
options = options || {}
// 这个时候这个行为就被称作 `mutate`,options 这个引用所指向的对象本身将会被修改
option.foo = options.foo || 'abc'
// 所以通常你的做法是新建一个变量来保存
var foo = options.foo || 'abc'
}
Online demo: http://jsbin.com/deqimaj/edit?js,console
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too