I am looking for an alternative equivalent solution for this jQuery-expression:
$.extend(true, {}, {foo: "bar"}, {bar: "foo"});
I am using Babel with ES_2015 and polyfill. For now I was assuming, that it is possible to use
Object.assign({}, {foo: "bar"}, {bar: "foo"});
In my case this is not what I was looking for, as I found out, that when a property is my own Class, that does not work.
For example
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);
It does not copy my Point class. Is there any other solution, leaving jQuery out?
Best regards,
Michael
edit2: Bergi was right, I got confused myself. I will do some testing, but for now its seems fine. Maybe I have a larger issue elsewhere in my code. Will get back to you. Thx so far folks edit, so nobody gets confused:
I NEED an instance of Point. Not an object.
/*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);
});
});
So in the end, I want s instanceof Point to be true ;)
Object.assign({}, a, b)?Pointinstance, I think.a,bandsare plain objects, that's what you've written.s.originands.sizeswill of course beinstanceof Point.