是否有与goog.object.extend相当的纯功能? [英] Is there a pure functional equivalent to goog.object.extend?
问题描述
根据关闭文件:
用另一个对象扩展一个对象。这是原地运作;它不创建一个新的对象。例如:var o = {}; goog.object.extend(o,{a:0,b:1}); O; // {a:0,b:1} goog.object.extend(o,{b:2,c:3}); O; // {a:0,b:2,c:3}
Extends an object with another object. This operates 'in-place'; it does not create a new Object. Example: var o = {}; goog.object.extend(o, {a: 0, b: 1}); o; // {a: 0, b: 1} goog.object.extend(o, {b: 2, c: 3}); o; // {a: 0, b: 2, c: 3}
但这不是一种功能性的方法,功能性方法更好。 Closure是否为此提供了更现代的东西?
But this is not a functional approach and for many contexts a functional approach is better. Does Closure offer something more modern for this?
eg
goog.object.extend(a, b);
变为
becomes
a = goog.object.extend(a, b);
推荐答案
您可以使用空对象创建新对象字面意思是用你想复制的对象进行扩展 - 合并:
You can create a new object by using an empty object literal to be extended with the objects that you want to copy-merge:
var x = {};
goog.object.extend(x, a, b);
a = x;
如果要扩展非简单对象,可能需要使用 Object .create(Object.getPrototypeOf(a))
。
If you are extending non-plain objects, you might need to use Object.create(Object.getPrototypeOf(a))
.
这篇关于是否有与goog.object.extend相当的纯功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!