History of succession
There are four forms in the history of succession, let's take a look.
1. Traditional forms
Fir.prototype.lastName = "STG"; function Fir () { this.age =; } var fir = new Fir (); Foo.prototype = Fir; function foo () {} var foo = new Foo (); Person.prototype = foo; function person () {}
From the code we can see that the code looks cumbersome, not beautiful, and inherited very cumbersome, so this form is out from the beginning.
Cons: Excessive inheritance of useless attributes.
2. With the help of constructors
function Person1 (name,age) { this.name = name; This.age = age; } function Person2 (name,age) { var pObj = object.create (person2.prototype); Person1.call (pobj,name,age); return pObj; } var person = new Person2 (' STG ', 23);
This approach is not inherently inherited, because he is borrowing call and constructors to change the constructor person's this point to himself, and not to access the prototype prototype.
Disadvantage: You cannot inherit the prototype of a borrowed constructor, and each constructor takes one more function.
3. Sharing prototypes
function inherit (origin,target) { target.prototype = Origin.prototype; } Person.prototype.lastName = "Xiaoming"; function person () {}; function Son () {} inherit (Person,son);
Cons: You can't change your prototype at will.
4. Grail mode
Due to the lack of a third way, I will add an intermediate layer in the middle as a transition, there is a fourth form of inheritance-------the Holy Grail pattern.
function inherit (origin,target) { function Func () {}; Func.prototype = Origin.prototype; Target.prototype = new Func (); } Person.prototype.lastName = "HHH"; function person () {} function Son () {} Inhert (person, Son);
Because the third form of the person and son prototype is pointing to the same prototype, so changing the person's prototype will be the son's prototype changes, here we add a middle-tier function func transition, let its prototype point to the origin of the
Prototype, then Target's prototype inherits its prototype, which is equivalent to the prototype of the target prototype inheriting origin, so that when son changes, the person's prototype will not change. One more drawback is that we know that the constructor attribute points to the
It is the function that constructs this object, but at this time son's constructor points to person, because the Func function does not have the constructor attribute, then will look up with the prototype chain, then found the person inside, this time we need
To manually add a constructor attribute to son.
In development, we need something called super-Class (Uber) to find out who the function is inheriting from. The final grail pattern is this:
function inherit (origin,target) { function Func () {}; Func.prototype = Origin.prototype; Target.prototype = new Func (); Target.prototype.constructor = Target; Target.prototype.uber = Origin.prototype; }
With the privatization variables of closures, we can write the Grail pattern a bit more advanced
var inherit = (function () { var Func = function () {}; return function (origin,target) { func.prototype = Origin.prototype; Target.prototype = new Func (); Target.prototype.constructor = Target; Target.prototype.uber = Origin.prototype; }} ())//**********function and function Func generate closures
name Space
function: manage variables to prevent contamination globally, for modular development
In the development, often each person uses the variable, the team development will have the variable conflict, we can use the object to solve, because each object inside the thing is independent, does not interfere with each other.
org = {Person : { xiaoming: { age:20, sex: "Male" }, Xiaozhang: { age:22, sex: "Fe Male " } }}var per1 = Org.person.xiaoming.age;var Per2 = org.person.xiaozhang.age;
using objects to resolve variable conflicts is a previously used method, and now we can use closures to solve the problem.
var add = (function () { var count1 = 1; var count2 = 0; function Myadd (NUM1, num2) { count1 = NUM1; Count2 = num2; Console.log (count1 + count2); } Return Myadd (Count1,count2); } ())
similar to privatization variables to resolve naming conflicts, only the variables that generate closures can be accessed.
JavaScript First (vi)--------inheritance history, namespaces