複製代碼 代碼如下: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);
結果顯示:男,張三,女,瑪麗