How to inherit and use non-function objects in Javascript

Source: Internet
Author: User

This article provides some detailed descriptions and examples of the inheritance and usage of non-function objects in Javascript. There are two types of Javascript inheritance. One is inheritance based on \ "function object \", that is, a function inherits another function.

The other is inheritance based on "non-function objects" and does not involve functions. Its approach is completely different from the previous case.

1. What is the inheritance of "non-function objects?

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

The Code is as follows: Copy code

Var Chinese = {

Nation: 'China'

};

Another object is called "Doctor ".

Var Doctor = {

Career: 'docker'

}

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

Ii. object () method

Douglas Crockford, inventor of json format, proposed an object () function to achieve this.

The Code is as follows: Copy code

Function object (o ){

Function F (){}

F. prototype = o;

Return new F ();

}

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:

The Code is as follows: Copy code

Var Doctor = object (Chinese );

Then, add the attributes of the sub-object:

The Code is as follows: Copy code

Doctor. career = 'Doc ';

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

The Code is as follows: Copy code

Alert (Doctor. nation); // China

Iii. 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:

The Code is as follows: Copy code

Function extendCopy (p ){

Var c = {};

For (var I in p ){

C [I] = p [I];

}

C. uber = p;

Return c;

}

Write as follows:

The Code is as follows: Copy code

Var Doctor = extendCopy (Chinese );

Doctor. career = 'Doc ';

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.

The Code is as follows: Copy code

Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];

Using the extendCopy () function, Doctor inherits Chinese.

The Code is as follows: Copy code

Var Doctor = extendCopy (Chinese );

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

The Code is as follows: Copy code

Doctor. birthPlaces. push ('xiamen ');

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

The Code is as follows: Copy code

Alert (Doctor. birthPlaces); // Beijing, Shanghai, Hong Kong, Xiamen

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.

Iv. 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.

The Code is as follows: Copy code

Function deepCopy (p, c ){

Var c = c || {};

For (var I in p ){

If (typeof p [I] === 'object '){

C [I] = (p [I]. constructor = Array )? []: {};

DeepCopy (p [I], c [I]);

} Else {

C [I] = p [I];

}

}

Return c;

}

Write as follows:

The Code is as follows: Copy code

Var Doctor = deepCopy (Chinese );

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

The Code is as follows: Copy code

Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];

Doctor. birthPlaces. push ('xiamen ');

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

The Code is as follows: Copy code

Alert (Doctor. birthPlaces); // Beijing, Shanghai, Hong Kong, Xiamen

Alert (Chinese. birthPlaces); // Beijing, Shanghai, Hong Kong

Currently, the jQuery library uses this inheritance method.

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.