JavaScript Learning: Basic inheritance mechanism

Source: Internet
Author: User
Tags inheritance php language lowercase reserved return
Javascript| inheritance recently because the school does the website design, therefore has been in the ASP and the database big expense painstakingly.
  
I was doing Java programming in the previous stage. I suddenly received the task and learned ASP, so I have been Dugezon and good at using JavaScript to frame ASP programs.
  
One of the obvious advantages of JavaScript is that it can define and hold its own objects. This seems to be unmatched by VBScript.
With this, JavaScript can be used to get closer to object-oriented programming. Maybe this will make website development more fun ...
  
But there's a serious flaw! JavaScript does not support inheritance mechanisms. Unlike Java, it supports extends keywords (although this keyword is reserved in JavaScript).
  
In Microsoft's ASP.net, JavaScript is only beginning to provide better support. The PHP language certainly also has the inheritance mechanism the support, these all let me favor ...
  
But now I can not convince the school's old men to buy a better domain space, but I do not want to endure the ASP no inheritance mechanism of the bitter, so improvisation, also have some results!
  
JavaScript simply does not support inheritance mechanisms! That's for sure. But we can find a way to do something and simulate one out.
Nonsense to say a bunch, first take a look at an example:

function person ()
{
Public://notice this public! In fact, there is no such use, it is only my habit. Fortunately, there is no mistake in practical application
This. Getname=person_mfgetname;

Private://As public, this is 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 only the first step!
Here is a key step: Inherit!

function Student ()//Extends Class:person
{
EXTENDS://My habit, but remember that you can't use lowercase letters. Because extends is a reserved word in JavaScript.
This. Super=person; The definition points to its parent class builder. The super here can't be in lowercase.
This. Super (); Call its parent class builder. This allows you to inherit all of the properties and methods from the parent class

Private
This.m_nstudentid=0;
}

Although the GetName () method is not seen in student, it can be invoked. Because he has inherited the person's GetName () method.

var mystudent=new Student ();
Mystudent.getname (); Note that the GetName method of its "parent class" is invoked, resulting in the return of "Guest".


This is true for JavaScript inheritance implementations. Just remember two steps:
  
1: In "subclass" first defines a function pointing to "parent class" (any name can be, I am accustomed to using super)
2: Then call this function
  
This allows you to inherit all the properties and methods of the parent class!
  
What I suspect now is that since extends and super are reserved words, why does JavaScript not support inheritance?
I wonder if there is any better way? I hope you will advise ...



Related Article

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.