Javascript Object-Oriented Programming (III): inheritance of non-Constructor

Source: Internet
Author: User

The first part of this series describes "encapsulation", and the second part describes how to use constructors to implement "inheritance ".
Today is the last part, which describes how to implement "inheritance" without using constructors ".
1. What is the inheritance of "non-constructor?
For example, there is an object called "Chinese ".
Var Chinese = {
Nation: 'China'
};
Another object is called "Doctor ".
Var Doctor = {
Career: 'docker'
}
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 ".
Ii. object () method
Douglas Crockford, inventor of json format, proposed an object () function to achieve this.
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:
Var Doctor = object (Chinese );
Then, add the attributes of the sub-object:
Doctor. career = 'Doc ';
At this time, the sub-object has inherited the attributes of the parent object.
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:
Function extendCopy (p ){
Var c = {};
For (var I in p ){
C [I] = p [I];
}
C. uber = p;
Return c;
}
Write as follows:
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.
Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];
Using the extendCopy () function, Doctor inherits Chinese.
Var Doctor = extendCopy (Chinese );
Then, we add a city for Doctor's "Birthplace:
Doctor. birthPlaces. push ('xiamen ');
What happened? The Chinese "Birthplace" has also been changed!
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.
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 it as follows: www.2cto.com
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:
Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];
Doctor. birthPlaces. push ('xiamen ');
In this case, the parent object will not be affected.
Alert (Doctor. birthPlaces); // Beijing, Shanghai, Hong Kong, Xiamen
Alert (Chinese. birthPlaces); // Beijing, Shanghai, Hong Kong
Currently, the jQuery library uses this inheritance method.

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.