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

How to dynamically add modules to Nginx

Written in front Often, after we install Nginx ba...

Detailed explanation of nginx optimization in high concurrency scenarios

In daily operation and maintenance work, nginx se...

Solution to MySQL Chinese garbled characters problem

1. The Chinese garbled characters appear in MySQL...

Using JS timer to move elements

Use JS timer to make an element to make a method ...

Detailed explanation of CSS label mode display property

The code looks like this: <!DOCTYPE html> &...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

How to use node to implement static file caching

Table of contents cache Cache location classifica...

The most commonly used HTML escape sequence

In HTML, <, >, &, etc. have special mean...

CSS3 changes the browser scroll bar style

Note: This method is only applicable to webkit-ba...

SELinux Getting Started

Back in the Kernel 2.6 era, a new security system...

How to install Android x86 in vmware virtual machine

Sometimes you just want to test an app but don’t ...

MySQL server 5.7.20 installation and configuration method graphic tutorial

This article records the installation and configu...

CSS3 to achieve menu hover effect

Result: html <nav id="nav-1"> <...