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

What does the legendary VUE syntax sugar do?

Table of contents 1. What is syntactic sugar? 2. ...

Pure CSS to achieve left and right drag to change the layout size

Utilize the browser's non- overflow:auto elem...

Steps to install GRUB on Linux server

How to Install GRUB for Linux Server You cannot u...

WeChat applet development practical skills: data transmission and storage

Combining the various problems I encountered in m...

Sample code for testing technology application based on Docker+Selenium Grid

Introduction to Selenium Grid Although some new f...

Detailed explanation of how two Node.js processes communicate

Table of contents Preface Communication between t...

js dynamically adds example code for a list of circled numbers

1. Add the ul tag in the body first <!-- Unord...

HTML CSS3 does not stretch the image display effect

1. Use the transform attribute to display the ima...

Front-end development must learn to understand HTML tags every day (1)

2.1 Semanticization makes your web pages better u...

mysql5.7.17 installation and configuration example on win2008R2 64-bit system

123WORDPRESS.COM has explained to you the install...

Solution to the problem that input in form cannot be submitted when disabled

I wrote a test program before, in which adding and...

JavaScript type detection method example tutorial

Preface JavaScript is one of the widely used lang...