0

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;
walther
  • 13,239
  • 5
  • 39
  • 64
Charnstyle
  • 93
  • 7

2 Answers2

0

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
Alexander
  • 19,443
  • 17
  • 61
  • 146
0

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;

here: http://jsfiddle.net/augusto1982/a8zjg1to/

Augusto
  • 761
  • 5
  • 18