Different ways of Creating Objects in Javascript

ref –

  • https://coderwall.com/p/p5cf5w/different-ways-of-creating-an-object-in-javascript
  • http://chineseruleof8.com/code/index.php/2019/10/04/pure-prototype-inheritance-with-object-create/
  • http://chineseruleof8.com/code/index.php/2018/04/13/object-create/

Object() constructor

Object.create()

a references an object, with its __proto__ pointing to undefined.

ref – http://chineseruleof8.com/code/index.php/2018/04/13/object-create/

Shorthand

b references an object, with its __proto__ pointing to default Object Prototype. Which has property constructor referencing to default Object function constructor. And of course, the default Object constructor has prototype reference back to the Object Prototype.

Using new to create instance

Given function

Explain this:

JS goes into 3 steps.
First, it identifies the function:

Second, it creates the instance:

Third, it initializes the instance:

which goes something like this:

All three steps is combined to create an instance object.

As a Singleton

or using IIFE

ref – http://chineseruleof8.com/code/index.php/2017/10/12/immediately-invoked-function-expression/