Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript"> <! --
ClassModel = // class model, used to create a class
{
Create: function ()
{
Return function () {this. construct. apply (this, arguments );}
}
}
Extend = function (desc, src) // simulate class inheritance, copy all the members of an object to another object
{
For (var c in src)
{
Desc [c] = src [c];
}
Return desc;
}
Object. prototype. extend = function (src)
{
Return Extend. apply (this, [this, src]);
}
Var human = ClassModel. create ();
Human. prototype =
{
Construct: function () // Constructor
{
// Alert ("construct method ");
// Alert (this. speak () + "," + this. sleep ());
},
Speak: function ()
{
Alert ("speak ");
},
Sleep: function ()
{
Alert ("sleep ");
},
Sex: function ()
{
Alert ("female ");
}
}
Var h = new human ();
H. speak (); // call the speak method of the human class
Var student = ClassModel. create ();
Student. prototype = (new human (). extend ({// student class inheritance class human class
Sex: function () // method overload (polymorphism)
{
Alert ("male ");
},
Study: function ()
{
Alert ("studying ");
},
Thinking: function ()
{
Alert ("thinking ");
}
});
Var student = new student ();
Student. sleep (); // call the sleep method of the parent class (human)
Student. study (); // call student's study method
Student. thinking (); // call the thinking method of student.
Student. sex (); // The result is that the male is no longer a parent girl.
// --> </Script>
</Head>
</Html>