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:
- Scope: private and privilege methods
- 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:
- Function constructor pattern allows to create public and private methods.
- I can access the Human.prototype properties.
- child object refers to Human prototype
__proto__
->[[prototype]]
(?)
- child object refers to Human prototype
Object.create I can only:
- 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!