JSOOP package mechanism, method definition for class creation _ javascript skills

Source: Internet
Author: User
Tags hasproperty
The JSOOP package mechanism defines the method of class creation. For more information, see. The Code is as follows:


/**
* Define a 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;
};



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 ';


/**
* Check 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;
};

/**
* Define a simulation class. It supports the package mechanism, inheritance, and polymorphism.
* @ Param packageName {String} package name
* @ Param className {String} Class Name
* @ Param superClass {Class} parent Class Object
* @ Param classImp {Object} class implementation code
* @ Param isOverride {Boolean} indicates whether to overwrite. If the class definition exists, it is not 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, 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 a class
$ This_class = $ this_pkg [className] = function (){};

// Point the subclass prototype to the parent class to obtain property 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 base class of the class created in the framework.
Core. Class. create (SYS_DEF_CLASS_NS, 'xclass', Object ,{
'Version': 'v0. 1'
});

// Xu. Core. Class Test Area
// Definition of test class coverage;
// Core. Class. create (SYS_DEF_CLASS_NS, 'xclass', Object ,{
// 'Version': 'v0. 5'
//}, True );
//
//// Method for checking test attributes;
// 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 );


Tested and used /...
Author: vb2005xu
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.