var jsobject = {} || new object();
jsobject.extend = function(subclass, superclass){
//先判断子类subclass是否已经定义,如果未定义,则重新定义类。
if(typeof subclass == undefined)subclass = function(){};
//如果父类superclass是类,则转化成对象
if(typeof superclass == function)superclass = new superclass();
//遍历父类superclass对象中的属性和方法
for(var p in superclass)
{
/*将父类superclass对象中的属性和方法复制到子类prototype对象中,
因此子类拥有父类的所有特性,即为继承 */
subclass.prototype[p] = superclass[p];
}
return subclass;
};
function student()
{
this.name = 张三;
this.updatename = function(name){
this.name = name;
}
}
function class1()
{
this.sex = 男;
this.updatesex = function(sex){
this.sex = sex;
}
}
//定义类class1继承student类
class1 = jsobject.extend(class1, student);
var obj = new class1();
alert(obj.sex);
alert(obj.name);
obj.updatesex(女);
obj.updatename(玛丽);
alert(obj.sex);
alert(obj.name);
结果显示:男,张三,女,玛丽
