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

A detailed account of the process of climbing a pit of Docker deployment service

First time writing. Allow me to introduce myself....

Simple Mysql backup BAT script sharing under Windows

Preface This article introduces a simple BAT scri...

Flame animation implemented with CSS3

Achieve results Implementation Code html <div ...

Detailed explanation of 7 SSH command usages in Linux that you don’t know

A system administrator may manage multiple server...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

Detailed explanation of the JavaScript timer principle

Table of contents 1. setTimeout() timer 2. Stop t...

WeChat applet calculator example

This article shares the specific code of the WeCh...

MySQL View Principle Analysis

Table of contents Updatable Views Performance of ...

How to install MySQL database on Debian 9 system

Preface Seeing the title, everyone should be thin...

Mobile browser Viewport parameters (web front-end design)

Mobile browsers place web pages in a virtual "...

A brief discussion on Nginx10m+ high concurrency kernel optimization

What is high concurrency? The default Linux kerne...

Detailed explanation of the reasons why MySQL connections are hung

Table of contents 1. Background Architecture Prob...

Summary of the use of MySQL date and time functions

This article is based on MySQL 8.0 This article i...

HTML page jump passing parameter problem

The effect is as follows: a page After clicking t...