Copy Code code as follows:
<script type= "Text/javascript" ><!--
Classmodel =//class model, for creating classes
{
Create:function ()
{
return function () {this.construct.apply (this,arguments);}
}
}
Extend = function (desc, SRC)//Impersonation class inheritance, copying all 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 (Polymorphic)
{
Alert ("Male");
},
Study:function ()
{
Alert ("studying");
},
Thinking:function ()
{
Alert ("thinking");
}
});
var student = new Student ();
Student.sleep (); Call the parent class (human) sleep method
Student.study (); Call the Student study method
Student.thinking (); Call the student thinking method
Student.sex (); The result is that a male is no longer a parent.
--></script>