First, what is inherited, "non-structural function"?
For example, now we have an object called "Chinese people."
var Chinese = { Nation: 'Chinese' };
Another object is called "programmer."
var Programmer = { Career: 'programmer' }
How can we let the "programmer" to inherit "Chinese people"?
Here two objects are ordinary objects, rather than the constructor can not be achieved "inheritance" with the constructor method.
Two, object () method
function object(o) { function F() {} F.prototype = o; return new F(); }
The object here () function, is a child object prototype property, point parent object, the child object so that together with the parent object.
When used, first on the basis of the parent object, the child object that generated:
var Programmer = object(Chinese);
Then, together with the property sub-object itself:
Programmer.career = 'programmer';
At this time, the child object has inherited the property of the parent object.
Alert (Programmer.nation); // China
Third, the shallow copy
The property of the parent object, all copies to child objects can also be achieved inheritance.
The following function, that is, make a copy:
function extendCopy(p) { var c = {}; for (var i in p) { c[i] = p[i]; } c.uber = p; return c; }
Write use:
var Programmer = extendCopy (Chinese); Programmer.career = 'programmer' ; Alert (Programmer.nation); // China
But there is a shallow copy problem: If the property is equal to an array parent or another object, in fact, just a memory address child objects obtained, rather than real copy, it may be tampered with parent presence.
Now add to the Chinese a "place of birth" property whose value is an array.
Chinese.birthPlaces = [ 'Beijing', 'Shanghai', 'Guangzhou'];
By extendCopy () function, Programmer inherited the Chinese.
var Programmer = extendCopy(Chinese);
Then we add a city for the Programmer's "birthplace":
Programmer.birthPlaces.push ( 'Shenzhen');
At this point, Chinese of "place of birth" has been modified?
Alert (Programmer.birthPlaces); // Beijing, Shanghai, Guangzhou, Shenzhen Alert (Chinese.birthPlaces); // Beijing, Shanghai, Guangzhou, Shenzhen
So, extendCopy () simply copies the basic types of data, we call this copy is called "shallow copy." This is an early inheritance jQuery way.
Fourth, deep copy
The so-called "deep copy", is the ability to copy arrays and objects to achieve the true sense. Its implementation is not difficult, as long as the recursive calls "shallow copy" on the line.
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; }
When using write:
were Programs = deepCopy (Chinese);
Now, to add a parent object property value array. Then, modify the properties on child objects:
Chinese.birthPlaces = [ 'Beijing', 'Shanghai', 'Guangzhou' ];
Programmer.birthPlaces.push ( 'Shenzhen');
At this time, the parent object will not be affected.
Alert (Programmer.birthPlaces); // Beijing, Shanghai, Guangzhou, Shenzhen Alert (Chinese.birthPlaces); // Beijing, Shanghai, Guangzhou
Currently, jQuery library uses this method to inherit.
As inherited constructor, refer https://www.cnblogs.com/Leophen/p/11134437.html