分配给在 ES5 中作为参数传递的对象
Assign to object passed as argument in ES5
我有一个对象作为参数传递给函数。在那个函数里面我想给它分配一个不同的值,但是因为在函数里面我们只有对原始对象的引用所以我们不能使用 =.
的简单赋值
在 ES2015 中我可以使用 Object.assign
除了将属性复制到引用之外,是否有我可以在 ES5 中使用的解决方法?
这是一个例子https://jsbin.com/wimuvaqosi/1/edit?js,console
var x = {prop:1};
function foo(x1) {
var y = {prop:2};
x1 = y; //this obviously does not work
//x1 = Object.assign(x1, y); //this works only in ES2015
}
foo(x);
console.log("x ", x);
Is there a workaround I could use in ES5, other than copying properties to the reference?
不是,Object.assign 也只是复制属性。但是你可以简单地使用 polyfill 并在你的 pre es2015 代码中使用 Object.assign():
if (typeof Object.assign != 'function') {
Object.assign = function(target) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source != null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
您似乎正试图将 x
完全更改为另一个对象。如果是这样,谢天谢地,你不能这样做。甚至 Object.assign(x, y)
也只是将属性从 x
复制到 y
就像@baao 的 polfill 一样。 x
仍然引用同一个对象。但是你可以:
var x = {prop:1};
function foo(x1) {
var y = {prop:2};
return y;
}
x = foo(x);
console.log("x ", x);
或
var x = {prop:1};
function foo(ref) {
var y = {prop:2};
ref.x = y;
}
var refX = {x: x};
foo(refX);
console.log("x ", refX.x);
IMO 这些解决方案比您正在寻找的更好,因为调用者可以看到变量 x
现在指向不同的对象 - 这可能很重要。例如,在 Javascript 语言中,调用者可能总是做出如下假设:
y = x;
foo(x);
y === x; // always true
我可以从查找表和其他结构中引用 x
,但这些不会被替换 - 它可能会变得非常混乱。在其他语言中,例如 C#,您可以通过引用传递对象,但是有一个不同的调用签名,例如 foo(ref x);
,因此您可以在阅读代码时轻松地看到 x 可能被替换。
我有一个对象作为参数传递给函数。在那个函数里面我想给它分配一个不同的值,但是因为在函数里面我们只有对原始对象的引用所以我们不能使用 =.
的简单赋值在 ES2015 中我可以使用 Object.assign
除了将属性复制到引用之外,是否有我可以在 ES5 中使用的解决方法?
这是一个例子https://jsbin.com/wimuvaqosi/1/edit?js,console
var x = {prop:1};
function foo(x1) {
var y = {prop:2};
x1 = y; //this obviously does not work
//x1 = Object.assign(x1, y); //this works only in ES2015
}
foo(x);
console.log("x ", x);
Is there a workaround I could use in ES5, other than copying properties to the reference?
不是,Object.assign 也只是复制属性。但是你可以简单地使用 polyfill 并在你的 pre es2015 代码中使用 Object.assign():
if (typeof Object.assign != 'function') {
Object.assign = function(target) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source != null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
您似乎正试图将 x
完全更改为另一个对象。如果是这样,谢天谢地,你不能这样做。甚至 Object.assign(x, y)
也只是将属性从 x
复制到 y
就像@baao 的 polfill 一样。 x
仍然引用同一个对象。但是你可以:
var x = {prop:1};
function foo(x1) {
var y = {prop:2};
return y;
}
x = foo(x);
console.log("x ", x);
或
var x = {prop:1};
function foo(ref) {
var y = {prop:2};
ref.x = y;
}
var refX = {x: x};
foo(refX);
console.log("x ", refX.x);
IMO 这些解决方案比您正在寻找的更好,因为调用者可以看到变量 x
现在指向不同的对象 - 这可能很重要。例如,在 Javascript 语言中,调用者可能总是做出如下假设:
y = x;
foo(x);
y === x; // always true
我可以从查找表和其他结构中引用 x
,但这些不会被替换 - 它可能会变得非常混乱。在其他语言中,例如 C#,您可以通过引用传递对象,但是有一个不同的调用签名,例如 foo(ref x);
,因此您可以在阅读代码时轻松地看到 x 可能被替换。