-1

I have

var shared = {
}

var stuff = Object.assign(Object.create(shared),{
   //stuff
});

But Object.assign doesn't work on Safari and I don't wanna use something like Babel since my website is already kinda laggy. Is there any good alternative to make it so I can do this while maintaining "stuff" inheritance to "shared"..

var shared = {
}

var stuff = Object.create(shared);
stuff = {//stuff
};

I realize I can simply assign properties one by one to "stuff" but I have a lot of properties there and it would make the code a lot less organized

Magdi Gamal
  • 681
  • 1
  • 9
  • 25

2 Answers2

0

If you want another way to copy properties, how about something like this (copies everything from A to B):

const a = {hello:1, world:2};
const b = {};
Object.keys(a).forEach(k => b[k] = a[k]);
// b is now {hello:1, world:2}.
wgoodall01
  • 1,855
  • 14
  • 21
  • this will not `clone` objects but it will hold references only – Tareq Jan 14 '18 at 02:30
  • Well, neither does `Object.assign()`. If you want to deep clone an object that's a different problem. – wgoodall01 Jan 14 '18 at 02:32
  • You are right, it will not. I though it would by default, at least for first level entries – Tareq Jan 14 '18 at 02:37
  • But since I'm also using Object.create() isn't what I'm doing less of a copy and more of just adding the first object to the second object prototype chain? Or is that basically the same thing? – Magdi Gamal Jan 14 '18 at 02:53
0

If you want quick dirty solution with minimal change to your codebase, try this pkg https://github.com/rubennorte/es6-object-assign

In my opinion, you should use bundling framework, say webpack, alongside with babel. There are many ways to reduce the bundled file size like tree-shaking, minifying, separating vendor files and sending JS files gzipped to the browser and it is not laggy at all.

Tareq
  • 5,283
  • 2
  • 15
  • 18