Home  >  Article  >  Web Front-end  >  JS OOP package mechanism, method definition of class creation_javascript skills

JS OOP package mechanism, method definition of class creation_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:42:391406browse
Copy code The code is as follows:

/**
* Definition package
* @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;
};


Copy code The code is as follows:

// ------------- ------ Xu.Core.Class namespace
Xu.pkg("Core.Class");
var SYS_DEF_CLASS_NS = 'Xu.Class.Sys' ;
var USER_DEF_CLASS_NS = 'Xu. Class.Custom' ;


/**
* Verify whether the class exists
* @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 ;
};

/**
* Simulation class definition, supports package mechanism, inheritance and polymorphism
* @param packageName {String} The name of the package
* @param className {String} The name of the class
* @param superClass {Class} parent class object
* @param classImp {Object} class implementation code
* @param isOverride {Boolean} whether to override, when the class definition exists, it will not be overwritten by default
*
* @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 an exception occurs, it means that the class is not defined
}

if (Core.Util.isNull(packageName) | | packageName === ""){
packageName = USER_DEF_CLASS_NS ;
}
$this_pkg = Xu.pkg(packageName);
//Define the parent class and point the prototype of the subclass to the parent class
if (Core.Util.isNull(superClass) || superClass === ""){
// superClass = Object ;
superClass = Xu.Class.Sys.XClass ;
}
//Define class
$this_class = $this_pkg[className] = function(){};

// Point the prototype of the subclass to the parent class to obtain attribute inheritance
$ 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 ;
} ;
//Define the base class for the classes created in the framework.
Core.Class.create(SYS_DEF_CLASS_NS,'XClass' ,Object,{
'version': 'V0.1'
});

// Xu.Core.Class test area
//Test class coverage definition;
//Core.Class.create(SYS_DEF_CLASS_NS,'XClass',Object,{
// 'version': 'V0.5'
//},true);
//
/ /// Test class property checking method;
//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. //
//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);


Used for testing, haha/...
Author: vb2005xu
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn