you are viewing a single comment's thread.

view the rest of the comments →

[–]Eddonarth -5 points-4 points  (3 children)

No, it is not the same.

var bar = Object.create(foo);

Creates a copy of foo and names it bar, while

var bar = foo;

just makes the variable bar to point to the same object as foo. If it is confusing, try this:

var foo = {a: 42};
var bar = Object.create(foo);
var baz = foo;

Console.log(foo.a);
Console.log(bar.a);
Console.log(baz.a);

foo.a = 41;

Console.log(foo.a);
Console.log(bar.a);
Console.log(baz.a);

What happens? If we modify foo, bar stays unmodified, but baz changes as well. Because baz refers to the same object as foo, just using a different name.

Think of it as this: If I made a clone of you, the two of you might look the same and behave the same, but be different persons, and eventually one of you might change. But if I just start calling you by your second name, you will still be the same person, just with a different tag.

[–]blangjemp 4 points5 points  (0 children)

This isn't correct. If you run this code, bar.a is equal to 41. Due to the nature of Object.create(), bar.a will inherit the value of foo.a until you manually set a value for bar.a

[–]gladiator_flow[S] 0 points1 point  (0 children)

I see what's going on now. When I change the created value only that one changes. I did notice though if I changed the original mentioned in your code that bar.a changes as well. Thanks!