您的位置:首页 > 其它

模仿mootools对象创建方法 简单的实现

2012-03-28 10:57 549 查看
var extend = function(destination, source, override) {
if (override === undefined) override = true;
for (var property in source) {
if (!override && typeof(source[property]) == 'function' && destination.hasOwnProperty(property)) {
destination[property] = (function(name, method) {
return function() {
this.base = source[name];
return method.apply(this, arguments);
}
})(property, destination[property]);
} else {
destination[property] = source[property];
}
}

return destination;
};

var Class = function(implements) {
if (typeof implements == 'undefined') return;

var newClass = function() {
extend(this, implements);
this.initialize.apply(this, arguments);
};

newClass.prototype.setOptions = function(opts) {
extend(this.options, opts || {});
return this;
};

return newClass;
};

var Tab = new Class({
options : {
name : 'test'
},
initialize : function(opts) {
this.setOptions(opts);
alert(this.options.name);
},
show : function() {
return 'i will show';
}
});

var t = new Tab({ name : 'newName' });
alert(t.show());


  

欢迎大家给改进意见!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐