Tutorials  /  JavaScript

Understanding the Prototype Design Pattern in JavaScript

Ccentron Redaktion · November 2023 ·3 min read ·JavaScript, Tutorial

Contents

  1. Using the Prototype Pattern
  2. The Revealing Prototype Pattern

The "Prototype Design Pattern" in JavaScript allows for creating clones of objects that can be used in different parts of an application without the need to repeat the expensive creation process each time. We'll walk you through the basics.

The Prototype Design Pattern is an important concept in JavaScript that is based on the principle of prototypical inheritance. Many JavaScript developers have encountered the "prototype" keyword, puzzled over prototypical inheritance, or implemented prototypes in their code. This pattern leverages the JavaScript prototype model to create objects in performance-intensive situations.

The created objects are clones (shallow clones) of the original object that can be passed around. One use case of the Prototype pattern is to avoid costly database operations for creating an object for other parts of the application. Instead of repeating the same costly operation, it can be advantageous to clone a previously created object.

Using the Prototype Pattern

To clone an object, a constructor must be present to instantiate the initial object. Subsequently, variables and methods are bound to the object's structure using the "prototype" keyword. Here is a simple example:

Code
var TeslaModelS = function() {
    this.numWheels    = 4;
    this.manufacturer = 'Tesla';
    this.make         = 'Model S';
}

TeslaModelS.prototype.go = function() {
    // Turn wheels
}

TeslaModelS.prototype.stop = function() {
    // Apply brake pads
}

The constructor allows for creating a single TeslaModelS object. When a new TeslaModelS object is created, it retains the states initialized in the constructor. Adding functions like "go" and "stop" is straightforward since we declared them using "prototype."

An alternative method for extending functions within the prototype is as follows:

Code
var TeslaModelS = function() {
    this.numWheels    = 4;
    this.manufacturer = 'Tesla';
    this.make         = 'Model S';
}

TeslaModelS.prototype = {
    go: function() {
    // Turn wheels
    },
    stop: function() {
    // Apply brake pads
    }
}
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €4.04 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

The Revealing Prototype Pattern

Similar to the module pattern, there is also a variant of the Prototype pattern known as the "Revealing Prototype Pattern." This variant allows for encapsulating public and private members by returning an object literal.

By returning an object, the prototype object name is associated with a function, enabling control over what should be exposed in the current prototype to manage access:

Code
var TeslaModelS = function() {
    this.numWheels    = 4;
    this.manufacturer = 'Tesla';
    this.make         = 'Model S';
}

TeslaModelS.prototype = function() {

    var go = function() {
    // Turn wheels
    };

    var stop = function() {
    // Apply brake pads
    };

    return {
    pressBrakePedal: stop,
    pressGasPedal: go
    }

}();

Note that the "stop" and "go" functions are encapsulated before the returned object because they are outside the scope of the returned object. Since JavaScript natively supports prototypal inheritance, there's no need to rewrite fundamental functions.

The Prototype Design Pattern is a powerful concept that enables efficient object creation in JavaScript and facilitates object reuse in complex applications. Understanding the Prototype Design Pattern

Claim €200 credit

Test your setup on ccloud³

Sign up for ccloud³ and receive €200 starting credit for your project.

centron Redaktion Technical editorial team

The centron editorial team writes guides from day-to-day operations: tested on our own platform, run in our data centre in Hallstadt near Bamberg.

Category JavaScript
Share
Still have questions?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

Was this tutorial helpful?

Your answer is stored anonymously and helps us improve our tutorials.

Comments

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Get started for free

Sign up and receive €200 credit at centron within your first 60 days.

This promotional offer applies to new accounts only. Available exclusively to businesses.