修正上篇原错误观点。
var X = {};
// 提供对象扩展方法
X.extend = function (target, source) {
var i = '';
for (i in source) {
if ( source.hasOwnProperty(i) ) {
target[i] = source[i];
}
}
return target;
};
Function.method('create', function (obj) {
var A = function () {};
A.inherits(this);
// 强制设置必须拥有的方法 当然最好可以靠约定 不过约定有时并不可靠
X.extend(A.prototype, X.Base.baseMethods);
X.extend(A.prototype, obj);
return A;
});
// 基类
X.Base = function () {};
X.Base.baseMethods = {
// 此处指定必须拥有的方法
log : function () {
console.log('Sorry,');
}
};
X.extend(X.Base.prototype, X.Base.baseMethods);
X.extend(X.Base.prototype, { /* 其他属性和方法 */ });
X.A = X.Base.create({
log : function () {
this.uber('log');
console.log('I\'m');
}
});
X.B = X.A.create({
log : function () {
this.uber('log');
console.log('wrong');
}
});
X.C = X.B.create({
log : function () {
this.uber('log');
console.log('.');
}
});
window.pageData = {
pageId : 'C'
};
X.init = function () {
( new X[window.pageData.pageId] () ).log();
};
X.init();