Tutorials  /  JavaScript

Understanding the Prototype Design Pattern in JavaScript

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

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 €3.12 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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren