Search within TutorialsPark

Javascript : Object Creation


Create Javascript object using multiple methods for various purposes.


Javascript Object Creation

Javascript objects can be created very easily using object constructor or object literals, but there is disadvantage to this as multiple objects with same interface means a lot of code duplication.

Hence, various variations of factory pattern are used as per requirements in each case.


Javascript Objects Creation: Factory Pattern

The factory pattern pattern is an abstract way to create specific objects. Its a well known design pattern in software engineering.

Here, the functions are created to encapsulate the creation of objects with specific interfaces.

In the below demo, the functions createCar() accepts arguments to build an object with sufficient properties to represent a car.

Example: Javascript Object Creation Factory Pattern

Give it a TRY! » Note: This Function can be called unlimited times with different arguments to return an object with three properties and one method.


Javascript Objects Creation: Constructor Pattern

Native constructors like object and array are automatically available to the user in execution enviornment during runtime.

To create custom constructors that define properties and methods to build your own type of objects use Constructor Pattern .

The difference between Factory Pattern and Constructor Pattern is as follows:

1. No object is created explicitly

2. The this object is assigned both the properties and methods.

3. There is no return statement to return an output.

Example 2: Javascript Objects Creation Constructor Pattern

Give it a TRY! » Note: This Function can be called unlimited times with different arguments to return an object with three properties and one method.


Javascript Objects Creation : Prototype Pattern

The Prototype Pattern is used to create an object containing properties and methods which is available to instances of a particular reference type.

This is the prototype of the object to be created when the constructor is called.The advantage of this pattern is that all of its properties and methods are shared among instance objects.

Here the object information is not assigned to the constructor but its assigned directly to the prototype, follow the example below.

Example : Javascript Objects Accessor Properties

Give it a TRY! » Note: The Object() constructor with keyword new is called to create an instance of the object called car.


Javascript Object Creation: Combination Constructor

Custom type objects are created by combining the constructor and prototype pattern. Here, the constructor pattern defines the instance properties, while the prototype pattern defines method and shared properties

The advantage of this method is that each instance has its own copy of instance properties, while they all share references to methods, hence conserving precious memory space.

The instance properties are defined solely in the constructor, and the shared property constructor and the method printBrand() are defined on the property

Example: Javascript Object Creation Combination Constructor

Give it a TRY! » Note: The Object() constructor with keyword new is called to create an instance of the object called car.


Javascript Object Creation: Dynamic Prototype Pattern

The Dynamic Prototype hold all information within the constructor, at the same time maintaining the benefits of both a constructor and a prototype by inititalizing a prototype within a constructor, when needed.

You can determine if the prototype needed to be initialized by checking for the existence of a method that should be available.

Example: Javascript Object Creation Dynamic Prototype pattern

Give it a TRY! » Note: The return value is an object.


Javascript Object Creation: Parasitic Constructor Pattern

The Parasitic Constructor pattern is used to create a constructor that wraps the creation and return of another object, but still look like a typical constructor.

This pattern should be used when other patterns fail as a fallback.

Example: Javascript Object Creation Parasitic Constructor pattern

Give it a TRY! » Note: Here, the Car constructor creates a new object, initalizes it with properties and methods and then returns the object.


Javascript Object Creation: Durable Constructor Pattern

The Durable Constructor pattern is used to create objects that have no public properties and whose methods do not reference the this object.

This pattern should be used in secure enviornments(where use of this or new is not allowed) or to secure the data from the rest of applications.

The Durable pattern differs from Parasitic pattern in two ways:

1. The instance methods created on the object do not refer to this.

2. The Constructor is never called using the operator new.

Example: Javascript Object Creation Dynamic Prototype pattern

Give it a TRY! » Note:The Car variable is a durable object, there is no way to access any of its data members without calling a method.