给阳光网写了一些js代码,想进行oo封装。毕竟对于常用的方法需要整理归纳,oo是比较理想合适。
参考了prototype的实现,感觉很不错,修改一下为:
var generateClass = function(properties, baseclass) {
var c = function() {
if (this.initialize) {
this.initialize.apply(this, arguments);
}
};
/**
* 类原型拓展
* @param {Object} methods
* @param {Object} spuerclass
*/
c.implement = function(methods, spuerclass) {
if (spuerclass) {
var parent = new spuerclass();
for (property in parent) {
this.prototype[property] = parent[property];
}
c.base = spuerclass.prototype;
}
if (methods) {
for (property in methods) {
this.prototype[property] = methods[property];
}
}
};
c.implement(properties, baseclass);
/**
* 类自身静态属性拓展
* @param {Object} methods
*/
c.extend = function(methods) {
for (property in methods) {
this[property] = methods[property];
}
};
return c;
};
$O = generateClass;
var c = function() {
if (this.initialize) {
this.initialize.apply(this, arguments);
}
};
/**
* 类原型拓展
* @param {Object} methods
* @param {Object} spuerclass
*/
c.implement = function(methods, spuerclass) {
if (spuerclass) {
var parent = new spuerclass();
for (property in parent) {
this.prototype[property] = parent[property];
}
c.base = spuerclass.prototype;
}
if (methods) {
for (property in methods) {
this.prototype[property] = methods[property];
}
}
};
c.implement(properties, baseclass);
/**
* 类自身静态属性拓展
* @param {Object} methods
*/
c.extend = function(methods) {
for (property in methods) {
this[property] = methods[property];
}
};
return c;
};
$O = generateClass;
使用也比较简单方便:
var c1 = $O({
name:''
});
var o1 = new c1();
在此就不累述。
有了这个,为阳光网方便实现了很多js。感觉上容易分析了。