Copy Code code as follows:
var jsobject = {} | | New Object ();
Jsobject.extend = function (subclass, superclass) {
First, determine if the subclass subclass is already defined, or redefine the class if it is undefined.
if (typeof subclass = = "undefined") subclass = function () {};
If the parent class superclass is a class, it is converted into an object
if (typeof superclass = = "function") superclass = new Superclass ();
Traversing properties and methods in the parent class superclass object
For (var p in superclass)
{
/* Copy properties and methods from the parent class superclass object into the subclass prototype object.
Therefore, the subclass has all attributes of the parent class, that is, inheritance.
SUBCLASS.PROTOTYPE[P] = superclass[p];
}
return subclass;
};
function Student ()
{
THIS.name = "John";
This.updatename = function (name) {
THIS.name = name;
}
}
function Class1 ()
{
This.sex = "male";
This.updatesex = function (Sex) {
This.sex = sex;
}
}
Define class CLASS1 inherit student class
Class1 = Jsobject.extend (Class1, Student);
var obj = new Class1 ();
alert (obj.sex);
alert (obj.name);
Obj.updatesex ("female");
Obj.updatename ("Mary");
alert (obj.sex);
alert (obj.name);
The results show: Male, John, female, Mary