JS OOPパッケージ構造、クラス作成の方法定義
4712 ワード
/**
*
* @param {} ns
* @return {}
*/
Xu.pkg = function(ns) {
if (!ns || !ns.length) {
return null;
}
var levels = ns.split(".");
var nsobj = Xu;
for (var i = (levels[0] == "Xu") ? 1 : 0; i < levels.length; ++i) {
nsobj[levels[i]] = nsobj[levels[i]] || {};
nsobj = nsobj[levels[i]];
}
return nsobj;
};
// ------------------- Xu.Core.Class
Xu.pkg("Core.Class");
var SYS_DEF_CLASS_NS = 'Xu.Class.Sys' ;
var USER_DEF_CLASS_NS = 'Xu.Class.Custom' ;
/**
*
* @param {String} Class
* @return {Boolean}
*/
Core.Class.isExist = function(Class){
if (Core.Util.isFunction(Class))
return true ;
return false ;
};
Core.Class.remove = function(Class){
if (Core.Class.isExist(Class))
Xu.pkg(Class.prototype.__pkg__)[Class.prototype.__class__] = null ;
};
Core.Class.hasProperty = function(Class,property){
if (Core.Class.isExist(Class))
if ( Class[property] || Class.prototype[property])
return true ;
return false ;
};
/**
* , ,
* @param packageName {String}
* @param className {String}
* @param superClass {Class}
* @param classImp {Object}
* @param isOverride {Boolean} , ,
*
* @return {Function}
*/
Core.Class.create = function(packageName,className,superClass,classImp,isOverride){
if (Core.Util.isNull(className) || className === ""){
return null ;
}
isOverride = isOverride || false ;
try {
var $this_class = eval(packageName + "." +className);
if (Core.Class.isExist($this_class)) {
trace( "isExist: " + className + " Override:" + isOverride );
if (!isOverride){
return null ;
}
}
}
catch(e){
// ,
}
if (Core.Util.isNull(packageName) || packageName === ""){
packageName = USER_DEF_CLASS_NS ;
}
$this_pkg = Xu.pkg(packageName);
// ,
if (Core.Util.isNull(superClass) || superClass === ""){
// superClass = Object ;
superClass = Xu.Class.Sys.XClass ;
}
//
$this_class = $this_pkg[className] = function(){};
// ,
$this_class.prototype = new superClass();
Object.extend($this_class.prototype ,
{
'__pkg__': packageName ,
'__superclass__': $this_class.prototype['__class__'] || 'Object',
'__class__': className ,
'toString': function(){
return "[class: " + this.__pkg__ + "." + this.__class__ + "]" ;
}
}
);
if (Core.Util.isObject(classImp)){
$this_class.prototype = Object.extend(
$this_class.prototype,classImp);
}
return $this_class ;
} ;
// , .
Core.Class.create(SYS_DEF_CLASS_NS,'XClass',Object,{
'version': 'V0.1'
});
// Xu.Core.Class
// ;
//Core.Class.create(SYS_DEF_CLASS_NS,'XClass',Object,{
// 'version': 'V0.5'
//},true);
//
//// ;
//Xu.Class.Sys.XClass.ve = '2' ;
//trace(Core.Class.hasProperty(Xu.Class.Sys.XClass,'ve'));
//
////Core.Class.remove(Xu.Class.Sys.XClass);
//
//var x_class = new Xu.Class.Sys.XClass();
//trace(x_class.toString() + x_class.version );
//traceobj('XClass',Xu.Class.Sys.XClass.prototype);
////
//var XClass_ = Core.Class.create(null,'XClass_',null,{h:'hello'});
////
//var x_class_ = new XClass_();
//trace(x_class_.toString() + x_class_.version );
//traceobj('XClass_',XClass_.prototype);
////
//var X_ = Core.Class.create(null,'X_',XClass_,null);
////
//var x_ = new X_();
//trace(x_.toString() + x_.version );
//traceobj('X_',X_.prototype);
テストに使うのは、ほほほ/…作者:v b 2005 xu