2

I'm not sure why this code is not working. I'm trying to use Object.create(); instead new

var Car = function() {
   console.log('Car Consctructor');
};

Car.prototype.color = 'red';

var bmw = Object.create(Car);

console.log(bmw.color); //Doesn't log red - ??
RuntimeException
  • 1,135
  • 2
  • 11
  • 25
  • I found that out, modified question. Thanks – RuntimeException Sep 18 '13 at 10:19
  • possible duplicate of [Using "Object.create" instead of "new"](http://stackoverflow.com/questions/2709612/using-object-create-instead-of-new) – Bergi Sep 18 '13 at 10:24
  • As bergi's link shows is that if you want to use Object.create with constructor functions you usually use it to set up inheritance. The way you use it now makes no sense. http://stackoverflow.com/a/16063711/1641941 – HMR Sep 19 '13 at 01:42

2 Answers2

4

Car is a function, Object.create() expects a prototype.

var bmw = Object.create(Car.prototype);
rid
  • 61,078
  • 31
  • 152
  • 193
0

You will have to pass the prototype to Object.create

var bmw = Object.create(Car.prototype);

Refer : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61