JavascriptOfInheritanceThere are two types. One is inheritance based on the "function object", that is, a function inherits another function. I have already introduced it yesterday.
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 ".
- 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.
- 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 as follows:
- 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.
Address: http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html