最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Assign to object passed as argument in ES5 - Stack Overflow

programmeradmin3浏览0评论

I have an object that I pass as an argument into a function. Inside that function I want to assign to it a different value, but because inside the function we have only reference to the original object we can not use simple assignment with =.

In ES2015 I could use Object.assign

Is there a workaround I could use in ES5, other than copying properties to the reference?

Here is an example ,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);

I have an object that I pass as an argument into a function. Inside that function I want to assign to it a different value, but because inside the function we have only reference to the original object we can not use simple assignment with =.

In ES2015 I could use Object.assign

Is there a workaround I could use in ES5, other than copying properties to the reference?

Here is an example https://jsbin./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);
Share Improve this question asked Jan 19, 2017 at 16:32 daniel.sedlacekdaniel.sedlacek 8,64910 gold badges48 silver badges80 bronze badges 2
  • In your example: x1.prop=2; you can modify every object to be equal to another. No need to recreate one – Jonas Wilms Commented Jan 19, 2017 at 16:37
  • 2 Object.assign does not assign x to a different object. It copies all properties of y to x. @baao's answer mimics 'Object.assign' exactly. – James Commented Jan 19, 2017 at 17:39
Add a ment  | 

2 Answers 2

Reset to default 8

Is there a workaround I could use in ES5, other than copying properties to the reference?

Not really, Object.assign also just copies properties. But you can simply use the polyfill and use Object.assign() in your pre es2015 code:

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;
  };
}

It looks like you are trying to change x to a different object pletely. If so you cannot, thankfully, do it. Even Object.assign(x, y) just copies properties from x to y just like @baao's polfill. x still references the same object. But you can:

var x = {prop:1};
function foo(x1) {
    var y = {prop:2};
    return y;
}
x = foo(x);
console.log("x ", x);

Or

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 these solutions are better than what you are looking for because the caller can see that variable x now points to a different object - and this might matter. For example in the Javascript language the caller may always make assumption such as:

 y = x;
 foo(x);
 y === x; // always true

I could have references to x from lookup tables and other structures, and these would not get replaced - it could get very confusing. In other languages, like C# you can pass object by reference, but there is a different call signature like foo(ref x); so you can easily see that x may be replaced when you read the code.

发布评论

评论列表(0)

  1. 暂无评论