JavaScript Play Inheritance (iii)

Source: Internet
Author: User
Tags inheritance

In the first two articles, we introduced the construction inheritance and the prototype inheritance. Now that we have finished writing the two remaining ones, these two applications are rarely applied to the first two, so I call them the Non-mainstream inheritance approach.

First, we look at Non-mainstream succession One: instance inheritance method.

I do not say so much nonsense, since it is Non-mainstream inheritance, it must not be used, since not commonly used still exist, there is only one factor, he used for specific occasions. Instance inheritance, which is mainly used for the inheritance of core objects, is the only way to resolve core object inheritance so far.

The core object of inheritance has a certain value, such as the Error object, our company may have to implement an error class to simplify future development, then I will use the instance inheritance method to inherit the error.

The code is as follows:

function ExtendingError(mes)
{
 var instance=new Error(mes);
 instance.NewError=function(){
  alert("Your Error is "+mes);
 }
 return instance;
}

Okay, under test:

var e=new ExtendingError("Your number is less than one");
e.NewError();
alert(e.toString());

The results satisfied US:

Good, nonsense not much to say, this is a non-mainstream way of inheritance, basically only for the core object of inheritance, remember!

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.