SoFunction
Updated on 2025-04-10

javascript object-oriented idea with source code


<html>
<head>
<script type="text/javascript"><!--
ClassModel = //Class Model, used to create classes
{
create: function()
{
return function(){(this,arguments);}
}
}
Extend = function(desc, src) //Mock class inheritance, copy all members of one object into another object
{
for(var c in src)
{
desc[c] = src[c];
}
return desc;
}
= function(src)
{
return (this, [this, src]);
}
var human = ();
=
{
construct : function() //Constructor
{
//alert("construct method");
//alert(() + "," + ());
},
speak : function()
{
alert("speak");
},
sleep : function()
{
alert("sleep");
},
sex : function()
{
alert("female");
}
}
var h = new human();
(); //Calling the spoke method of the human class
var student = ();
= (new human()).extend({ //student class inherits the human class
sex : function() //Method overload (polymorphism)
{
alert("male");
},
study : function()
{
alert("studying");
},
thinking : function()
{
alert("thinking");
}


});
var student = new student();
(); //Calling the sleep method of the parent class (human)
(); //Calling student's study method
(); //Calling the thinking method of student
(); //The result is that male is no longer a parent girl

// --></script>
</head>
</html>