In javascript, what is the difference between:
var a = { steak:5, soup:2 };
var b = Object.create(a);
and
var a = { steak:5, soup:2 };
var b = a;
In javascript, what is the difference between:
var a = { steak:5, soup:2 };
var b = Object.create(a);
and
var a = { steak:5, soup:2 };
var b = a;
The difference is that a is the prototype of b, not the same object.
var a = { steak:5, soup:2 };
var b = a;
b.peas = 1;
console.log(a.peas); // 1
vs.
var a = { steak:5, soup:2 };
var b = Object.create(a);
b.peas = 1;
console.log(a.peas); // undefined
When you use create you are creating a new object with the given prototype. When you use the = operator, you don't create a new object, you just copy its reference to another variable.
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
You can test your
var a = { steak:5, soup:2 };
var b = Object.create(a);
// vs
var a = { steak:5, soup:2 };
var b = a;