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

SSH port forwarding to achieve intranet penetration

The machines in our LAN can access the external n...

Implementation of CSS loading effect Pac-Man

emmm the name is just a random guess 2333 Preface...

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and rever...

MySQL sql_mode analysis and setting explanation

When inserting a set of data into the MySQL datab...

mysql 5.7.5 m15 winx64.zip installation tutorial

How to install and configure mysql-5.7.5-m15-winx...

Linux kernel device driver advanced character device driver notes

/****************** * Advanced character device d...

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as...

Using text shadow and element shadow effects in CSS

Introduction to Text Shadows In CSS , use the tex...

jQuery plugin to achieve seamless carousel

Seamless carousel is a very common effect, and it...

How to hide and remove scroll bars in HTML

1. HTML tags with attributes XML/HTML CodeCopy co...

HTML meta explained

Introduction The meta tag is an auxiliary tag in ...