1

The title may sound a bit confusing. Please allow me to cast Crockford's constructor into the following simple example and create objects using two different ways. (Browser is FireFox.)

var car = function(carSpec) {

  var maker = carSpec.maker;
  var year = carSpec.year;
  var that = {};

  that.getMaker = function () {
    return maker;
  };

  that.getYear = function () {
    return year;
  };

  return that;
};

One way to create an object, as Crockford pointed out, is to use Object.create method,

myCar = Object.create(car({maker: 'Nissan', year: 2004}));
console.log(myCar); // Object {}, on FireFox console.

and the methods getMaker and getYear are attached to the __proto__.

The other way is to invoke car and let it return an object

yourCar = car({maker: 'Ford', year: 2010});
console.log(yourCar); // Object { getMaker: car/that.getMaker(), getYear: car/that.getYear() }

and methods getMaker and getYear becomes the own properties of object yourCar.

My questions is: What are the pros and cons of these two ways of object creation from this "Crockford constructor"?

X...
  • 173
  • 1
  • 2
  • 12

2 Answers2

1

It makes no sense to call Object.create when you already have the complete object that you want. Inheritance is only useful when you have multiple objects that are supposed to share common properties, but in your example getMaker and getYear are own properties of each car instance.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks for pointing this out. But I was asking a different question. – X... Nov 04 '17 at 11:31
  • @Xavier I agree that the `Object.create` makes no sense, since calls of `car` already return real distinct objects(no overlapping property). – JJPandari Nov 04 '17 at 12:23
  • @ PanJunjei For this specific example, I, too, agree with Bergi. But, that was not the question. – X... Nov 04 '17 at 12:37
  • @Xavier What was the question then? I thought you were asking to compare your two ways of object construction in your code snippets. – Bergi Nov 04 '17 at 13:20
  • @ Bergi Right. Too simple an example to include possible "inheritance" behaviour. The question could perhaps be better rephrased as: How to compare attaching properties to the proptotype chain with assigning them as own properties? – X... Nov 04 '17 at 13:45
  • @Xavier For that, see [`prototype` vs `this`](https://stackoverflow.com/q/310870/1048572) (it doesn't matter whether you use `new` + `.prototype` or `Object.create` for the instantiation) – Bergi Nov 04 '17 at 13:48
0

The advantage of having methods in __proto__ or prototype is multiple copies of created objects will point to the same methods. Meaning only one copy of each method will be created. Less(relatively) usage of memory. If the methods are attached to the objects itself then each object will have its own copy of the methods.

Pratheep
  • 936
  • 1
  • 7
  • 17
  • So, you are basically saying that `Object.create` in this scenario - applying to this Crockford style constructor - is the same as the more conventional approach relying on `.prototype` and `new`. Is this a correct characterisation? – X... Nov 04 '17 at 13:00
  • In a way yes if you are creating instances without any inheritance. Otherwise here's a good answer(https://stackoverflow.com/questions/17392857/benefits-of-using-object-create-for-inheritance) on using `Object.create` for inheritance. – Pratheep Nov 04 '17 at 13:18
  • Nice post. Perhaps I prefer "delegation" more than "inheritance". My observation is that, in JavaScript, this term "inheritance" sugar-coats the underlying "delegation" accomplished by prototype chain lookup. – X... Nov 04 '17 at 13:34