0

So I am coming from classical OOP language and trying to wrap my head around javascript prototypical style.

Trying to understand the difference between function constructor pattern and Object.create pattern when it comes to:

  1. Scope: private and privilege methods
  2. When to use which in application

Function Constructor I can create private functions and methods as follows:

function Human() {
  this.public = "public accessible variable";
  let private = "private only accessible inside Human";
}
Human.prototype.speak = "hahahaha";

var child = new Human();
Console.log(child.public) // prints
console.log(child.private) // don't print 

Benefits:

  1. Function constructor pattern allows to create public and private methods.
  2. I can access the Human.prototype properties.
    • child object refers to Human prototype __proto__ -> [[prototype]] (?)

Object.create I can only:

  1. Create an object and set its prototype i.e. __proto__ to Human object (instead of Human's prototype)

But so what? What are the practical advantages of directly setting child's prototype to human over constructor function?

Examples of real uses would help!

Shaz
  • 1,443
  • 1
  • 27
  • 67
  • JS doesn't have public, privileged or private methods. JS objects only have properties, and that's it. – Bergi Jan 12 '19 at 12:35
  • Forget about `__proto__`. It's deprecated and should no longer be used, in code or for explanation. Use *[[prototype]]* as the name for the internal property in explanations, and `Object.get/setPrototypeOf` in code. – Bergi Jan 12 '19 at 12:37
  • Well, the advantage of `Object.create` is that it allows you to create objects with inheritance relationships *without needing to define or call a function*. It's a language primitive. Nothing more, nothing less. Of course, for the very common "object creation and initialisation" pattern (like in your example) you don't need that, you will want to use a constructor function. – Bergi Jan 12 '19 at 12:39
  • Ah i see. Can you please give me a practical example where you'd use object.create (apart from serializing). My issue when would i use .create vs constructor – Shaz Jan 12 '19 at 12:47
  • 1
    See the duplicates on when to choose which (and also consider ES6 `class` for simplicity). The most common case for using `Object.create` is [building ES5-style class inheritance](https://stackoverflow.com/q/12592913/1048572), but it can also be used [for myriads of other things](https://stackoverflow.com/a/28783238/1048572) (including [wanting to be explicit how instances are created](https://stackoverflow.com/a/33710351/1048572)). – Bergi Jan 12 '19 at 12:53

1 Answers1

1

Calling a constructor function:

 const child = new Human();

is (nearly) the same as:

 const child = Object.create(Human.prototype);
 Human.call(child);

therefore I would not see Object.create as a language feature, but rather as a way to understand prototypal inheritance in JS.

There are very very limited usecases for prototype chains without constructors. One example would be the deserialization of a Human:

 const serialized = JSON.stringify(child); // Human inheritance gets lost, its a plain object now

 const child2 = Object.assign(Object.create(Human.prototype), JSON.parse(serialized));
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • When you say, " very limited usecases for prototype chains without constructors". Can you please give me an example of using prototype with a constructor? And its advantage? – Shaz Jan 12 '19 at 11:34
  • @shaz I used it a few times, however I can't remember for what exactly (except for deserialization) – Jonas Wilms Jan 12 '19 at 11:38
  • So like 90% of times do you just use the classical oop constructor function pattern for objects? New to javascript I trying to figure out if I missed a 'trick' with object.create – Shaz Jan 12 '19 at 11:41
  • @shaz nowadays i use `class` 90% of the time – Jonas Wilms Jan 12 '19 at 11:42
  • Which is essential an es6 way of doing constructor functions with a few nuances when it comes to this and super keywords/dynamic scope.. – Shaz Jan 12 '19 at 11:46