Details on overriding prototype methods in JavaScript instance objects

Details on overriding prototype methods in JavaScript instance objects

In JavaScript , we can usually simply define a class like the following code:

var sample = function() {
    // constructor code here 
}

sample.prototype.func1 = function() {
    // func1 code here
}

sample.prototype.func2 = function() {
    // func2 code here
}

/* more sample prototype functions here... */

Then use the following code to instantiate and access the prototype method:

var sampleInstance = new sample();
sampleInstance.func1();
sampleInstance.func2();
// call more sample object prototype functions


But if we want to rewrite one of the prototype methods without destroying the original sample object, how to achieve it? The simplest way is to build another class to inherit sample , and then rewrite the base class method in the prototype method of the inherited class, as follows:

var subSample = function() {
    // constructor code here
}

// inherit from sample
subSample.prototype = new sample();
subSample.prototype.fun1 = function() {
    // overwrite the sample's func1
}

However, if you do not build an inherited class and want to rewrite the prototype method, you can directly use the following code:

var sampleInstance = new sample();
sampleInstance.func1 = function() {
    sample.prototype.fun1.call(this); // call sample's func1
    // sampleInstance.func1 code here
}


We redefined the func1 method of sample instance object, accessed its prototype method func1 , and then added some additional code to it. In this way, we extend the prototype method of sample without creating a derived class or destroying the prototype method of sample .

This is the end of this article about the details of overriding prototype methods in JavaScript instance objects. For more information about overriding prototype methods in JavaScript instance objects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript adds prototype method implementation for built-in objects
  • Take you to a thorough understanding of the prototype object in JavaScript
  • Comprehensive analysis of prototypes, prototype objects, and prototype chains in js
  • Things to note when using prototype objects in js

<<:  XHTML Getting Started Tutorial: Form Tags

>>:  Solve the error during connect exception in Docker

Recommend

Detailed explanation of js event delegation

1. Each function is an object and occupies memory...

Analyze MySQL replication and tuning principles and methods

1. Introduction MySQL comes with a replication so...

Nginx reverse proxy springboot jar package process analysis

The common way to deploy a springboot project to ...

An audio-visual Linux distribution that appeals to audiophiles

I recently stumbled upon the Audiovisual Linux Pr...

How to install vncserver in Ubuntu 20.04

Ubuntu 20.04 has been officially released in Apri...

Detailed tutorial on compiling and installing MySQL 8.0.20 from source code

In the previous article, we introduced: MySQL8.0....

13 JavaScript one-liners that will make you look like an expert

Table of contents 1. Get a random Boolean value (...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...

Summary of the dockerfile-maven-plugin usage guide

Table of contents pom configuration Setting.xml c...

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

CSS setting div background image implementation code

Adding background image control to a component re...

Core skills that web front-end development engineers need to master

The content involved in Web front-end development...

MySQL Daemon failed to start error solution

MySQL Daemon failed to start error solution A few...

MYSQL METADATA LOCK (MDL LOCK) MDL lock problem analysis

1. Introduction MDL lock in MYSQL has always been...