var JsObject = {} || new Object();
JsObject.extend = function(subClass, superClass){
// First determine whether the subclass subClass has been defined. If not, redefine the class.
if(typeof subClass == "undefined")subClass = function(){};
//If the parent class superClass is a class, convert it into an object
if(typeof superClass == "function") superClass = new superClass();
// Traverse the attributes and methods in the superClass object of the parent class
for(var p in superClass)
{
/* Sum the attributes and methods in the superClass object of the parent class The method is copied to the subclass prototype object,
Therefore, the subclass has all the characteristics of the parent class, which is inheritance*/
subClass.prototype[p] = superClass[p];
}
return subClass;
};
function Student()
{
this.name = "Zhang San";
this.updateName = function(name){
this.name = name ;
}
}
function Class1()
{
this.sex = "Male";
this.updateSex = function(sex){
this.sex = sex;
}
}
//Definition class Class1 inherits the Student class
Class1 = JsObject.extend(Class1, Student);
var obj = new Class1();
alert (obj.sex);
alert(obj.name);
obj.updateSex("女");
obj.updateName("Mary");
alert(obj.sex);
alert(obj.name);
The results show: male, Zhang San, female, Mary