Object.assign(... as)更改输入参数 [英] Object.assign(...as) changes input parameter
问题描述
Object.assign(... as)
出现以更改输入参数.示例:
const as = [{a:1},{b:2},{c:3}];const aObj = Object.assign(... as);
我将对象文字的数组解构为assign函数的参数.我省略了 console.log
语句.这是来自节点13.7的标准输出:
与之前一样:[{a:1},{b:2},{c:3}]
aObj:{a:1,b:2,c:3}
分配后:[{a:1,b:2,c:3},{b:2},{c:3}]
读者可能会注意到,作为第一个元素的 as
已被整体更改.将新的数组 bs
元素更改为不可变的对象(使用 freeze
)
const bs = [{a:1},{b:2},{c:3}];[0,1,2] .map(k => Object.freeze(bs [k]));const bObj = Object.assign(... bs);
导致错误:
TypeError:无法添加属性b,对象不可扩展在Function.assign(< anonymous>)
这表明该参数确实已被更改.
真正让我感到困惑的是,即使将我的数组 cs
绑定到一个函数上(我认为您在JS中称其为闭包)
const cs = [{a:1},{b:2},{c:3}];const f =(xs)=>Object.assign(... xs);const g =()=>f(cs);const cObj = g();
返回:
cs分配前:[{a:1},{b:2},{c:3}]cObj:{a:1,b:2,c:3}分配后的cs:[{{a:1,b:2,c:3},{b:2},{c:3}]
这里出了什么问题?以及如何安全地使用 Object.assign
而又不破坏其第一个参数?
Object.assign
不是纯函数,它会覆盖其第一个参数 target
.>
此处是 MDN :
Object.assign(target,... sources)
参数
目标
目标对象-应用于源属性的对象,修改后将返回该对象.
来源
源对象—包含要应用的属性的对象.
返回值
目标对象.
关键词是"[目标]被修改后返回 ".为避免这种情况,请将空对象文字 {}
作为第一个参数传递:
const aObj = Object.assign({},... as);
Object.assign(...as)
appears to change the input parameter. Example:
const as = [{a:1}, {b:2}, {c:3}];
const aObj = Object.assign(...as);
I deconstruct an array of object literals as parameter of the assign function.
I omitted console.log
statements. Here's the stdout from node 13.7:
as before assign: [ { a: 1 }, { b: 2 }, { c: 3 } ]
aObj: { a: 1, b: 2, c: 3 }
as after assign: [ { a: 1, b: 2, c: 3 }, { b: 2 }, { c: 3 } ]
The reader may notice that as
first element has been changed in an entire.
Changing a new array bs
elements to an immutable object (using freeze
)
const bs = [{a:1}, {b:2}, {c:3}];
[0, 1, 2].map(k => Object.freeze(bs[k]));
const bObj = Object.assign(...bs);
leads to an error:
TypeError: Cannot add property b, object is not extensible at Function.assign (<anonymous>)
Which indicates the argument is indeed being changed.
What really confounds me is that even binding my array, cs
, by currying it to a function (I think you call this a closure in JS)
const cs = [{a:1}, {b:2}, {c:3}];
const f = (xs) => Object.assign(...xs);
const g = () => f(cs);
const cObj = g();
returns:
cs before assign: [ { a: 1 }, { b: 2 }, { c: 3 } ] cObj: { a: 1, b: 2, c: 3 } cs after assign: [ { a: 1, b: 2, c: 3 }, { b: 2 }, { c: 3 } ]
What went wrong here? And how may one safely use Object.assign
without wrecking its first argument?
Object.assign
is not a pure function, it writes over its first argument target
.
Here is its entry on MDN:
Object.assign(target, ...sources)
Parameters
target
The target object — what to apply the sources’ properties to, which is returned after it is modified.
sources
The source object(s) — objects containing the properties you want to apply.
Return value
The target object.
The key phrase is "[the target] is returned after it is modified". To avoid this, pass an empty object literal {}
as first argument:
const aObj = Object.assign({}, ...as);
这篇关于Object.assign(... as)更改输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!