Reputation: 1135
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
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
Reputation: 63452
Car
is a function, Object.create()
expects a prototype.
var bmw = Object.create(Car.prototype);
Upvotes: 4