Javascript transfer inheritance (3) _ javascript skills

Source: Internet
Author: User
In the first two articles, we introduced constructor inheritance and prototype inheritance. Today, I have written the remaining two types of applications. These two types of applications have very few applications compared with the first two. Therefore, I call them a non-mainstream inheritance method. First of all, let's look at the non-mainstream inheritance one: instance inheritance law.

I will not talk so much nonsense. Since it is a non-mainstream inheritance, it will not be commonly used. Since it is not often used and still exists, there is only one factor. It is used for specific occasions. The instance inheritance method is mainly used for the inheritance of core objects. It is also the only way to solve the inheritance of core objects so far.

The inheritance of core objects has some value. For example, for the Error object, our company may need to implement an Error class to simplify future development. In this case, we will use the instance inheritance method to inherit the Error.

The Code is as follows:

The Code is as follows:


Function ExtendingError (mes)
{
Var instance = new Error (mes );
Instance. NewError = function (){
Alert ("Your Error is" + mes );
}
Return instance;
}

Good. test:

The Code is as follows:


Var e = new ExtendingError ("Your number is less than one ");
E. NewError ();
Alert (e. toString ());

The results satisfied us:

Well, this is not a mainstream inheritance method. It is basically only used for the inheritance of core objects. Remember it!
Next let's take a look at the non-mainstream inheritance II: copy the inheritance law.

As the name implies, copy inheritance means that objects are inherited through copy. What should we copy? Obviously, it is the attributes and methods of the object. Do you still remember that in Javascript, the class is actually a Hashtable? If you cannot remember it, go back and review the basics. I may write an article about Javascript objects in a while.
Once you understand this, you can simply look at the Code:
First, write an Extend method:

The Code is as follows:


Function. prototype. Extend = function (){
For (var pro in obj)
{
// In this way, the attributes and methods of the parent class are completely copied.
This. prototype [pro] = obj [pro];
}
}

Now, write a code segment to see how to use it:

The Code is as follows:


Function Animal ()
{}
Function People ()
{}
People. Extend (new Animal ())
{}

Obviously, the disadvantages of this method are too obvious:
When copying the object's attribute methods one by one, reflection is actually used, and reflection damages efficiency.
Like prototype inheritance, parent class objects must be initialized. When the inheritance relationship is determined, but the parameters are still unknown, you can skip this step!

In short, this method is generally not used.

Now, let's talk about some common things. Hybrid inheritance!
This is based on two mainstream inheritance methods. By comparing the two inheritance methods, we can find that the advantages and disadvantages of the two inheritance methods are complementary, so it's easy to mix them together!

The Code is as follows:


Function People (name)
{
This. name = name;
This. SayName = function (){
Alert ("My name is" + name );
}
}
Function Girl (name, age)
{
// Construct inheritance
This. father = People;
This. father (name );
Delete this. father;
This. Introduce = function (){
Alert ("My name is" + name + ". I am" + age );
}
}
// Prototype inheritance
Girl. prototype = new People ();
Okay, the two methods are mixed. Now let's see if the problem is solved?
Var g = new Girl ("Xuan", 22 );
Alert (g instanceof People );
G. SayName ();
G. Introduce ();


Test passed!

This is a relatively perfect solution, but it increases the complexity of the code, so the specific solution depends on everyone to choose in practice.

This is the method of Javascript inheritance. You are welcome to continue to pay attention to other articles.

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.