How to implement the prototype pattern in JavaScript

How to implement the prototype pattern in JavaScript

Overview

The prototype pattern refers to the type of object created by the prototype instance, and new objects are created by copying these prototypes. It is a pattern used to create objects, that is, to create an object as the prototype property of another object;

Prototype warning: Before learning about the prototype mode, you need to first learn about prototype, prototype chain, prototype, __proto__, constructor and other knowledge;

Implementing the Prototype Pattern

ES5 API: Object.create(prototype, optionalDescriptorObjects)

The Object.create() method receives two parameters: the first parameter is the __proto__ object, and the second is the prototiesObject (optional, the second parameter can be used to initialize additional properties, accepting literal object form);

var vehiclePrototype = {
    model:"Porsche",
    getModel: function () {
        console.log('The vehicle model is: ' + this.model);
    }
};

var vehicle = Object.create(vehiclePrototype,{
    "model":{
        value:"Ferrari"
    }
});

vehicle.getModel(); //Vehicle model is: Ferrari

We use Object.create to actually create a new object vehiclePrototype, and inherit the methods of vehiclePrototype, so at this time vehicle.__proto__ == vehiclePrototype;

The value of "model" is initialized in the second parameter, and the value of model is initialized to "Ferrari", so at this time there is only one model in the newly created object vehiclePrototype, and the value is "Ferrari";

Instead of using Object.create(), use prototype:

var vehiclePrototype = {
    init: function (carModel) {
        this.model = carModel || "Porsche";
    },
    getModel: function () {
        console.log('The vehicle model is: ' + this.model);
    }

};

function vehicle(model) {
    function F() { };
    F.prototype = vehiclePrototype;    
    var f = new F();
    f.init(model);
    return f;
}
var car = vehicle('Ferrari');
car.getModel(); // The vehicle model is: Ferrari

In the above code, we can see that the core is in vehicle. We first create a new constructor, then point the prototype of the function to vehiclePrototype, then instantiate the function, and finally call the init method on prototype after inheritance. Finally, returning the execution result can also be achieved;

Summarize

The prototype pattern is ubiquitous in JavaScript, and many other patterns are also implemented based on prototype, so it is important to study and review the knowledge of prototype and prototype chain, with an emphasis on key attribute knowledge points such as prototype, __proto__, constructor, etc.

The above is the details of how to implement the prototype pattern with JavaScript. For more information about the JavaScript prototype pattern, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • JavaScript Design Pattern Command Pattern
  • JavaScript Design Patterns – Command Pattern Principles and Usage Example Analysis
  • Detailed explanation of the principles and usage examples of JavaScript command mode
  • JavaScript design pattern command pattern example analysis
  • Analysis of the concept and usage of command mode in JS design pattern
  • JS command mode example menu program
  • JavaScript design pattern classic command pattern
  • In-depth understanding of JavaScript series (34): Detailed explanation of the command mode of design pattern
  • How to implement the observer pattern in JavaScript
  • Detailed explanation of the command mode in Javascript practice

<<:  How to install and configure Redis in CentOS7

>>:  Summary of common problems and application skills in MySQL

Recommend

js to realize a simple puzzle game

This article shares the specific code of js to im...

How are Vue components parsed and rendered?

Preface This article will explain how Vue compone...

MySQL Null can cause 5 problems (all fatal)

Table of contents 1. Count data is lost Solution ...

Steps to install RocketMQ instance on Linux

1. Install JDK 1.1 Check whether the current virt...

How to install Element UI and use vector graphics in vue3.0

Here we only focus on the installation and use of...

A brief analysis of HTML space code

How much do you know about HTML? If you are learni...

Apache Bench stress testing tool implementation principle and usage analysis

1: Throughput (Requests per second) A quantitativ...

About Generics of C++ TpeScript Series

Table of contents 1. Template 2. Generics 3. Gene...

Detailed explanation of gcc command usage under Linux system

Table of contents 1. Preprocessing 2. Compilation...

Example of using CSS filter to write mouse over effect

Use CSS filter to write mouse over effect <div...

Detailed explanation of basic concepts of HTML

What is HTML? HTML is a language used to describe...

How to use mysql to complete the data generation in excel

Excel is the most commonly used tool for data ana...

MySQL data operation-use of DML statements

illustrate DML (Data Manipulation Language) refer...