In-depth analysis of JavaScript Object-Oriented Programming (3)

Source: Internet
Author: User

Iii. Javascript Object-Oriented Programming: inheritance of non-Constructor

This section describes how to implement "inheritance" without using constructors ".

1. What is the inheritance of "non-constructor?

For example, there is an object called "Chinese ".

    1. VaRChinese = {
    2. Nation:'China' 
    3. };

Another object is called "Doctor ".

    1. VaRDoctor = {
    2. Career:'Docker' 
    3. }

How can I let "Doctors" inherit "Chinese"? That is to say, how can I generate a "Chinese doctor" object?

Note that these two objects are common objects, not constructor, and cannot be used to implement "inheritance ".

2. Object () method

Douglas crockford, inventor of JSON format, proposed an object () function to achieve this.

    1. FunctionObject (o ){
    2. FunctionF (){}
    3. F. Prototype = O;
    4. Return NewF ();
    5. }

This object () function is actually only used to direct the prototype attribute of the Child object to the parent object so that the child object can be connected with the parent object.

When used, the first step is to generate sub-Objects Based on the parent object:

    1. VaRDoctor = Object (Chinese );

Then, add the attributes of the sub-object:

    1. Doctor. Career ='Docker';

At this time, the sub-object has inherited the attributes of the parent object.

    1. Alert (doctor. Nation); // China

3. Shallow copy

In addition to the prototype chain, there is another idea: copying all attributes of the parent object to the sub-object can also realize inheritance.

The following function is used for copying:

    1. FunctionExtendcopy (p ){
    2. VaRC = {};
    3. For(VaRIInP ){
    4. C [I] = P [I];
    5. }
    6. C. Uber = P;
    7. ReturnC;
    8. }

Write as follows:

    1. VaRDoctor = extendcopy (Chinese );
    2. Doctor. Career ='Docker';
    3. Alert (doctor. Nation );// China 

However, there is a problem with such a copy. That is, if the attribute of the parent object is equal to an array or another object, in fact, the sub-object only obtains a memory address rather than a real copy, so there is a possibility that the parent object will be tampered.

Now, add a "Birthplace" attribute to Chinese. Its value is an array.

    1. Chinese. birthplaces = ['Beijing','Shanghai','Hong Kong'];

Using the extendcopy () function, Doctor inherits Chinese.

    1. VaRDoctor = extendcopy (Chinese );

Then, we add a city for doctor's "Birthplace:

    1. Doctor. birthplaces. Push ('Xiamen');

What happened? The Chinese "Birthplace" has also been changed!

    1. Alert (doctor. birthplaces );// Beijing, Shanghai, Hong Kong, Xiamen 
    2. Alert (Chinese. birthplaces );// Beijing, Shanghai, Hong Kong, Xiamen 

Therefore, extendcopy () is just a copy of the basic type of data. We call this copy "shortest copy ". This is an early Method for jquery to implement inheritance.

4. Deep copy

The so-called "Deep copy" means to copy arrays and objects in the true sense. Its implementation is not difficult, as long as the recursive call of "shortest copy.

  1. FunctionDeepcopy (p, c ){
  2. VaRC = C | {};
  3. For(VaRIInP ){
  4. If(TypeofP [I] ='Object'){
  5. C [I] = (P [I]. constructor = array )? []: {};
  6. Deepcopy (P [I], C [I]);
  7. }Else{
  8. C [I] = P [I];
  9. }
  10. }
  11. ReturnC;
  12. }

Write as follows:

    1. VaRDoctor = deepcopy (Chinese );

Now, add an attribute to the parent object and the value is an array. Then, modify this attribute on the sub-object:

    1. Chinese. birthplaces = ['Beijing','Shanghai','Hong Kong'];
    2. Doctor. birthplaces. Push ('Xiamen');

In this case, the parent object will not be affected.

    1. Alert (doctor. birthplaces );// Beijing, Shanghai, Hong Kong, Xiamen 
    2. Alert (Chinese. birthplaces );// Beijing, Shanghai, and Hong Kong 

Currently, the jquery library uses this inheritance method.

Link: http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html

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.