Home > Web Front-end > JS Tutorial > JavaScript: basic inheritance mechanism

JavaScript: basic inheritance mechanism

高洛峰
Release: 2016-11-25 11:26:30
Original
858 people have browsed it

 I was doing Java programming in the previous stage. I suddenly received a task and learned ASP, so I have always been keen on and good at using JavaScript to build ASP programs.
 
 An obvious advantage of JavaScript is that it can define and hold its own objects. This seems to be incomparable to VBScript.
 With this, JavaScript can be used for programming that is closer to object-oriented. Maybe this will make website development more fun...
 
 But there is a serious drawback! JavaScript does not support inheritance mechanism. Unlike Java, it does not support the extends keyword (although this keyword is a reserved word in JavaScript).
 
In Microsoft’s ASP.NET, JavaScript has only begun to provide relatively complete support. Of course, the PHP language also has the support of inheritance mechanism, which makes me favor...
 
But now I can't convince the old men at the school to buy a better domain name space, but I don't want to endure the pain of no inheritance mechanism in ASP, so Out of desperation comes wisdom, and some results have been achieved!
 
 JavaScript does not support the inheritance mechanism at all! Definitely. But we can think of ways to do some tricks and simulate it.
After talking a lot of nonsense, let’s look at an example first:

function Person()
{
public: // Pay attention to this public! In fact, there is no such usage, this is just my habit. Fortunately, it will not happen in practical applications Wrong
this.GetName=Person_mfGetName;

private: // Like public, this is also my habit
this.m_strName="Guest";
}

function Person_mfGetName()
{
return this.m_strName;
}

var MyPerson=new Person();
MyPerson.GetName();

You can use any output statement to view the results. Of course this is just the first step!
Here is the key step: inheritance!

function Student() // Extends Class: Person
{
EXTENDS: // My habit, but remember not to use lowercase letters. Because extends is a reserved word in JavaScript
this.Super=Person; // Definition points to Its "parent class constructor". Super here cannot be in lowercase
this.Super(); // Call its "parent class constructor". In this way, you can "inherit" all properties and attributes from the "parent class" Method

private:
this.m_nStudentID=0;
}

Although the GetName() method is not seen in Student, it can be called. Because he has inherited Person's GetName() method.

var MyStudent=new Student();
MyStudent.GetName(); // Note that the GetName method of its "parent class" is called, and the result is to return "Guest".


This is how JavaScript inheritance is implemented. Just remember two steps:
 
 1: First define a function pointing to the "parent class" in the "subclass" (any name can be used, I am used to using Super)
 2: Then call this function
 
 This way you can inherit" All properties and methods of the parent class"!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template