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,466
  • 5
  • 41
  • 67
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,906
  • 19
  • 75
  • 162
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
  • 779
  • 5
  • 18