Atitit. Principles for implementing inheritance and methods Java JavaScript. NET C # php ...

Source: Internet
Author: User

Atitit. Principles for implementing inheritance and methods java JavaScript. NET C # php ...

1. Implementation of inheritance issues 1

2. How do I copy a base class method? Using the prototype prototype, by impersonating object 1

3.2. How do properties inherit? 2

4. Comparison of several inheritance methods 2

5. Common Apply, call method 3

6. Reference 3

1. Implementation of inheritance issues


to implement inheritance in JavaScript is to implement three layers of meaning:
1.instances of subclasses can share methods of parent class;
2, subclasses can override the parent class method or extend the new method;
3, subclasses, and parent classes are " types "of child class instances.

To resolve two questions: 1. How do I copy a base class method? 2. How do attributes inherit?


in JavaScript, inheritance is not supported directly from the syntax, but it is possible to implement inheritance through a simulated approach, following a summary of several ways to implement inheritance:
1. Structural Inheritance Law
2. Prototype Inheritance Law
3. Example Inheritance Law
4. Copy Inheritance Law

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

2. How do I copy a base class method? Using the prototype prototype method, passing the impersonating object

1. For question 1, the prototype prototype is generally used to impersonate an object.

/* Change member's prototype property to implement inheritance */

09

Member.prototype=new User ();

2. Copy base class method code

1

Member.prototype=new User ();

The implication of this line of code is that the new user object, which passes this reference to the prototype of member, completes the copy work of the base class method. (Note: Here the code must be executed first, and then add a new method).

3.2.How do properties inherit?

For question 2, the call to the base class constructor is generally used

/* Initialize base class parameters */

06

User.call (This,userid,uname,uage,uscore);

1. Calling the base class constructor code

1

User.call (This,userid,uname,uage,uscore);

The meaning of this line of code is to execute the constructor of the user class and pass the member's this pointer field to the user class, when the user class runs the constructor, this points to the member object.

4. Comparison of several inheritance methods

implementation of inheritance in from JavaScript -Xieex- Blog Park . htm

Compare items

Construct inheritance

Prototype inheritance

Instance inheritance

Copy inheritance

static property inheritance

N

Y

Y

Y

Built-in (core) object inheritance

N

Part

Y

Y

Multi-argument multiple inheritance

Y

N

Y

N

Execution efficiency

High

High

High

Low

Multiple inheritance

Y

N

N

Y

instanceof

False

True

False

False

5. The commonly usedApply, callMethod

Each method has its own environment, for example, if the parent class has a parameter constructor: In this case, prototype is not applicable, you can choose apply or call;

How do you choose between apply and call? In Oo inheritance, a subclass inherits from the parent class, and it should be the type of the parent class. That is, Childclassbycall, childclassbyapply should also be ParentClass type, but we will detect with "instanceof", the subclass inherited by apply, not The ParentClass type. Therefore, we recommend using call + prototype to simulate implementation of inheritance. It is said that the Google Map API's inheritance is to use this way yo.

Apply implementation

Reference:: JavaScript inheritance Simulation Implementation-Yak-blog Park. htm

6. Reference

JavaScript implementation class, inheritance, Polymorphism (original) _ Tillage bibliography. htm

Implementation of inheritance in JavaScript-Xieex-blog Park. htm

Atitit. Principles for implementing inheritance and methods Java JavaScript. NET C # php ...

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.