JavaScript: Basic Inheritance Mechanism

Source: Internet
Author: User
JavaScript: the Basic Inheritance Mechanism I designed Java programs in the previous stage. I suddenly got a job and learned ASP, so I have always been loyal and good at using JavaScript to construct ASP programs.
  
One obvious advantage of JavaScript is that it can define and hold its own objects. This is incomparable to VBScript.
With this, JavaScript can be used for object-oriented programming. Maybe this will make website development more fun...
  
But there is a serious drawback! JavaScript does not support inheritance. Unlike Java, the extends keyword is supported (although this keyword is reserved in JavaScript ).
  
In Microsoft's ASP. NET, JavaScript began to provide comprehensive support. Of course, the PHP language also supports the Inheritance Mechanism. These are all called me...
  
But now I cannot convince the school's elders to buy a better domain name space, but I also don't want to endure the absence of inheritance mechanisms in ASP, so I am so anxious to have some results!
  
JavaScript does not support inheritance at all! This is for sure. But we can try to simulate it.
Let's take a look at an example:

Function Person ()
{
Public: // pay attention to this public! In fact, there is no such usage, which is just my habit. Fortunately, there will be no mistakes in practical applications.
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 result. Of course, this is only the first step!
The following is a key step: Inherit!

Function Student () // Extends Class: Person
{
EXTENDS: // my habits, but remember not to Use lowercase letters, because extends is a reserved word in JavaScript.
This. Super = Person; // The definition points to its "parent class constructor". The Super here cannot be in lowercase.
This. Super (); // call its "parent class constructor". In this way, all attributes and methods can be inherited from "parent class ".

Private:
This. m_nStudentID = 0;
}

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

Var MyStudent = new Student ();
MyStudent. GetName (); // note that the GetName method of its "parent class" is called and the result is "Guest ".


The implementation of JavaScript inheritance is like this. Keep two steps in mind:
  
1: first define a function pointing to "parent class" in "subclass" (any name is acceptable, I am used to using Super)
2: then call this function.
  
In this way, all attributes and methods of the "parent class" can be inherited!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.