0

Premise: I'm try to abbandon constructor pattern

As you can see in looking at console.log() here

//Constructor pattern
function F(){}

F.prototype.foo = null;

console.log(new F());

//Object create pattern
var FPrototype = {};

FPrototype.foo = null;

console.log(Object.create(FPrototype))

the object created by Object.create API has the __proto__ properties referencing the prototype as Object while the one created by constructor has the __proto__ prop referencing the prototype as the name of the constructor function.

I suppose that this behaviour is trying to simulate the strong typed langs, assuming that your constructor defines a new type identified by the constructor name itself.

That said, when my prototype chain grows I find very useful to identify different prototypes by name/"type", so is there any way to correctly have this using Object.create instead of constructor?

georg
  • 211,518
  • 52
  • 313
  • 390
Nemus
  • 1,322
  • 2
  • 23
  • 47
  • Yes, just add `FPrototype.constructor = F` to get a "typed" prototype. – georg Jan 26 '16 at 12:13
  • 1
    @georg: as I understand the question, he doesn't want `FPrototype` to inherit from `F`, rather he wants an object created from `FPrototype` to be marked as inheriting from `FPrototype` in a debugger. Which is, as far as I know, impossible (it would require something like `FPrototype.constructor = FPrototype` but this of course doesn't work as expected). – Wiktor Zychla Jan 26 '16 at 13:39
  • 1
    @WiktorZychla: how about `FPrototype.constructor = function FPrototype() {};` then? – georg Jan 26 '16 at 14:05
  • @georg: looks clever, haven't thought of redefining the `FPrototype` this way. Definitely looks better in the debugger, at least in Chrome. The newly created object still looks like `Object` but its prototype is now `FPrototype`. – Wiktor Zychla Jan 26 '16 at 18:13

0 Answers0