Copy Code code as follows:
Using prototype inheritance, a temporary object is used as the prototype property of the child, and the prototype property of the temporary object points to the prototype of the parent class.
Prevents all subclass and parent-class stereotype properties from pointing to a pass object.
This will not affect other subclasses and parent classes when you modify the stereotype properties of subclasses
function extend (child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F ();
Child.prototype.constructor = child;
Child.base = Parent.prototype;
}
function Parent (name)
{
THIS.AA = 123;
This.getname = function () {return name;}; Using closures to simulate private members
This.setname = function (value) {name=value;};
}
Parent.prototype.print = function () {alert ("print!");
Parent.prototype.hello = function ()
{
Alert (This.getname () + "Parent")
};
function Child (Name,age)
{
Parent.apply (this, arguments);//Call the parent class constructor to inherit the properties defined by the parent class
This.age = age;
}
Extend (child,parent); Inherit parent
Child.prototype.hello = function ()//rewrite the parent Hello method
{
Alert (This.getname () + "Child");
Parent.prototype.hello.apply (this,arguments); Calling the parent class with the same name method
};
Subclass method
Child.prototype.doSomething = function () {alert (this.age + "Child dosomething");
var p1 = new Child ("Xhan", 22);
var P2 = new Child ("xxx", 33);
P1.hello ();
P2.hello ();
P1.dosomething (); Subclass method
P1.print (); Parent class method
Alert (P1 instanceof child); True
Alert (P1 instanceof Parent);//true