【问题标题】:JS Object.assign with own classes as propertiesJS Object.assign 具有自己的类作为属性
【发布时间】:2016-06-05 13:08:07
【问题描述】:

我正在为此 jQuery 表达式寻找替代的等效解决方案:

$.extend(true, {}, {foo: "bar"}, {bar: "foo"});

我正在使用带有 ES_2015 和 polyfill 的 Babel。现在我假设,可以使用

Object.assign({}, {foo: "bar"}, {bar: "foo"});

就我而言,这不是我想要的,因为我发现,当一个属性是我自己的类时,这不起作用。

例如

let a = {origin: new Point(0,0), sizes: new Point(100, 100)};
let b = {origin: new Point(50,50), sizes: new Point(200, 200)};

Object.assign({}, a, b);

它不会复制我的 Point 类。有没有其他的解决方案,把 jQuery 排除在外?

最好的问候,

迈克尔

edit2:Bergi 是对的,我自己也很困惑。我会做一些测试,但现在看起来还不错。也许我在代码的其他地方有一个更大的问题。会回复你的。到目前为止,谢谢大家 编辑,所以没有人会感到困惑:

我需要一个 Point 的实例。不是对象。

    /*global Point*/
describe('Object.assign', function() {
    "use strict";

    it("Point", function() {
        let a = {
            origin: new Point.Point(0, 0),
            sizes: {
                x: new Point.Point(100, 100),
                y: new Point.Point(500, 500)
            }
        };
        let b = {
            origin: new Point.Point(50, 50),
            sizes: {
                x: new Point.Point(1000, 1000),
                y: new Point.Point(5000, 5000)
            }
        };
        var s = Object.assign({}, a, b);
        console.log(typeof s.origin, s.origin instanceof Point.Point);
        console.log(typeof s.sizes.x, s.sizes.x instanceof Point.Point);
        console.log(typeof s.sizes.y, s.sizes.y instanceof Point.Point);

        console.log(s.sizes.y.clone, s.sizes.x.clone);

    });

});

所以最后,我希望 s instanceof Point 为真 ;)

【问题讨论】:

  • 它似乎对我有用。 Object.assign({}, a, b) 到底能得到什么?
  • 我认为 OP 希望以 Point 实例结束。
  • 是的,我想在合并对象中拥有我的 Point 实例
  • 你的问题真的没有意义。 abs 是普通对象,这就是你写的。 s.origins.sizes 当然是 instanceof Point

标签: javascript jquery performance ecmascript-6


【解决方案1】:

我认为,它需要实现“克隆”方法。但是 ES2015 没有这个方法...

(jQuery有clone方法,但我知道你不想用它...)

我的实现如下,请用它来实现:)

function clone(obj) {
  if (null == obj || "object" != typeof obj) return obj;
  var copy = new obj.constructor;
  for (var attr in obj) {
    if (obj.hasOwnProperty(attr)) {
      if (null == obj[attr] || "object" != typeof obj[attr]) {
        copy[attr] = obj[attr];
      } else {
        copy[attr] = clone(obj[attr]);
      }
    }
  }
  return copy;
}

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

let a = {origin: new Point(0,0), sizes: new Point(100, 100)};
let b = {origin: new Point(50,50), sizes: new Point(200, 200)};

const copied = Object.assign({}, clone(a), clone(b)); // <- copied!

console.log(copied); // { origin: Point { x: 50, y: 50 }, sizes: Point { x: 200, y: 200 } }
b.origin.x = 300;    // <- changed!
console.log(b);      // { origin: Point { x: 300, y: 50 }, sizes: Point { x: 200, y: 200 } } (b was changed)
console.log(copied); // { origin: Point { x: 50, y: 50 }, sizes: Point { x: 200, y: 200 } } (but, copied isn't changed!)

【讨论】:

  • 这不是我想要的。这只会复制一个对象。我想复制实例
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2020-03-28
  • 2015-06-27
相关资源
最近更新 更多