日期:2014-05-16 浏览次数:20347 次
function Base() {}
Base.extend = function(obj) {
//顶级构造
var Class = obj['constructor'];
//解析传入的对象实现构造函数
for(var i in obj) {
if(i != "constructor") {
Class.prototype[i] = obj[i];
}
}
Class.extend = function(objChild) {
var ChildClass = objChild['constructor'];
//继承父类的prototype方法
ChildClass.prototype = this.prototype;
//提供base方法调用父类的构造
ChildClass.prototype.base = function() {
obj['constructor'].apply(this, arguments)
}
//解析传入的对象实现构造函数
for(var k in objChild) {
if(k != "constructor") {
ChildClass.prototype[k] = objChild[k];
}
}
//子类的子类也有继承的特性
ChildClass.extend = Class.extend;
return ChildClass;
}
return Class;
}
var Person = Base.extend({
constructor: function(name) {
this.name = name;
},
getName : function() {
return this.name;
}
});
var User = Person.extend({
constructor: function(name, password) {
this.base(name);
this.password = password;
},
getPassword : function() {
return this.password;
}
});
var ArchiveUser = User.extend({
constructor: function(name, password, isAdmin) {
this.base(name, password);
this.isAdmin = isAdmin;
},
getAdmin : function() {
return this.isAdmin;
}
});
var archiveUser = new ArchiveUser("Jack", "123", 1);
alert(archiveUser.getPassword());
function Base() {}
Base.extend = function(obj) {
//顶级构造
var Class = obj['constructor'];
//解析传入的对象实现构造函数
for(var i in obj) {
if(i != "constructor") {
Class.prototype[i] = obj[i];
}
}
&nbs