Since JavaScript is OOP programming, there must be a concept of inheritance. Internet search a bit, more chaotic, here I do a summary.
There are roughly three ways:
1, Apply/call, can not inherit the prototype;
2, prototype, the parent constructor has parameters when it loses the meaning of inheritance, it becomes a new object;
3, Apply/call and prototype mixed use.
Among them, the most common is the third, the code is as follows:
function A () {
this.age=10;
}
A.prototype.name= "Jack";
function B () {
a.apply (this);//Inherit A's own property
this.sex= "Remale";
}
B.prototype = Object.create (A.prototype);//very critical, create an empty object to inherit the prototype of the parent class to ensure that the parent class is not modified
//b.prototype = a.prototype;//must not write this, Writing this will change the value of the attribute on the parent prototype.
B.prototype.constructor = b;//This sentence has no effect on the structure, just to inherit the consistency of the structure, otherwise the construtor points to a.
var b = new B ();
Alert ("An attribute on the archetype of a:" +b.name);
Alert ("A's own property:" +b.age);
Alert ("B's Own property:" +b.sex);
The previous two methods have problems, do not paste, attached link: http://blog.csdn.net/kkkkkxiaofei/article/details/46474069