JavaScript object-oriented class inheritance case explanation

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance

In the above chapters, we have seen that JavaScript's object model is based on prototype implementation. Its characteristic is simplicity, but its disadvantage is that it is more difficult to understand than the traditional class-instance model. The biggest disadvantage is that the implementation of inheritance requires writing a lot of code and correctly implementing the prototype chain.

Is there a simpler way to write it? have!

insert image description here

Let's first review how to implement Student using functions:

    function Student(name) {
        this.name = name;
    }

    // Now we need to add a method to this Student Student.prototype.hello = function () {
        alert('Hello, ' + this.name + '!');
    }
    
    Student.prototype.hello.apply(new Student("Xiao Ming")); 

insert image description here

If we use the new class keyword to write Student, we can write it like this:

insert image description here

Finally, create a Student object with the same code as in the previous section:

insert image description here

Class inheritance

insert image description here

This is the end of this article about JavaScript object-oriented class inheritance case. For more relevant JavaScript object-oriented class inheritance content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • js learning notes: class, super and extends keywords
  • JS quickly master ES6 class usage
  • Two ways to write JS tab plugins (jQuery and class)
  • Detailed explanation of adding and deleting class names using JS
  • Class in front-end JavaScript

<<:  MySQL 8.0.17 installation and simple configuration tutorial under macOS

>>:  Detailed steps for setting up the network for the virtual machine that comes with win10 (graphic tutorial)

Recommend

Reasons and solutions for failure to insert emoji expressions in MySQL

Failure Scenario When calling JDBC to insert emoj...

JavaScript to achieve product magnifying glass effect

This article shares the specific code of JavaScri...

Detailed explanation of Nginx process management and reloading principles

Process structure diagram Nginx is a multi-proces...

Solve the error of installing VMware Tools on Ubuntu 18.04

1. According to the online tutorial, the installa...

Meta tags in simple terms

The META tag, commonly referred to as the tag, is...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

CSS imitates Apple's smooth switch button effect

Table of contents 1. Code analysis 2. Source code...

Ubuntu installation graphics driver and cuda tutorial

Table of contents 1. Uninstall the original drive...

Detailed tutorial of using stimulsoft.reports.js with vue-cli

vue-cli uses stimulsoft.reports.js (nanny-level t...

On Visual Design and Interaction Design

<br />In the entire product design process, ...

Some findings and thoughts about iframe

This story starts with an unexpected discovery tod...

Implementation of form submission in html

Form submission code 1. Source code analysis <...

React hooks introductory tutorial

State Hooks Examples: import { useState } from &#...

How to use CSS counters to beautify ordered lists of numbers

In web design, it is very important to use an org...