RuntimeException
RuntimeException

Reputation: 1135

Object.create not working as expected

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 - ??

Upvotes: 2

Views: 2160

Answers (2)

Clyde Lobo
Clyde Lobo

Reputation: 9174

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

Upvotes: 1

rid
rid

Reputation: 63452

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

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

Upvotes: 4

Related Questions