JavaScript class and Namespace impersonation code _js object-oriented

Source: Internet
Author: User
Tags anonymous class definition
The simplest of the first paragraph:
Copy Code code as follows:

The following lines of code show the impersonation definition and use of namespaces, classes, and functions:
NameSpace = {};
Namespace.class = function () {
This. method = function (info) {alert (info);}
};
New Namespace.class (). Method ("Hello World");

Some more visible, various cases of code
1. Simulation of class
Copy Code code as follows:

Class definition
function Class (info) {
Private members
var privatedata = "Private Data";
var privatemethod = function () {WriteLine ("Private");
function PrivateMethod2 (Info) {WriteLine ("Private");}
Public members (use this)
This. data = "public data";
This. method = function () {WriteLine (info);
};
Static members of a class
Class.staticdata = "static data";
Class.staticmethod = function (info) {WriteLine (info);


2. Simulation of the name space
Copy Code code as follows:

function NameSpace () {}
Or
NameSpace = {};
Or
NameSpace = new Object ();

3. Goal: Create class instances and invoke instance methods
Copy Code code as follows:

var o = new Namespace.class ("Hello World");
O.method ();
Use an existing class definition and hang it under namespace with a static method
Namespace.class1 = Class;
New Namespace.class1 ("New Namespace.class1 ()." Method ()). Method ();
Or: Create a new class definition
Namespace.class2 = function (info) {
This. method = function () {WriteLine (info);
};
New Namespace.class2 ("New Namespace.class2 ()." Method ()). Method ();


4. Goal: Calling static functions of a class
Copy Code code as follows:

NameSpace.Class.StaticMethod ();
Static Object + static method
NAMESPACE.CLASS3 = {}; {} indicates that this is an object, or with the new object ();
NameSpace.Class3.Method = function (info) {WriteLine (info);
NameSpace.Class3.Method ("NameSpace.Class3.Method ()");
Or: New an object to give static members
NAMESPACE.CLASS4 = new Class ("NameSpace.Class4.Method ()");
NameSpace.Class4.Method ();
Or: The anonymous function is used to define the class and then create the object with new
NAMESPACE.CLASS5 = new (function (info) {
This. method = function () {WriteLine (info);
}) ("NameSpace.Class5.Method ()");
NameSpace.Class5.Method ();
Or: Json method (class definition + create simultaneous completion)
The advantage is simple, the disadvantage is not to pass the parameters in
NAMESPACE.CLASS6 = {
Method:function (Info) {WriteLine (info);}
};
NameSpace.Class6.Method ("NameSpace.Class6.Method ()");

Demo Code:
<ptml> <pead> </pead> <body> <p>javascript class and namespace simulation </p> <p><b> see Source code and track test </b></p> <p> collation collection:<i>http://surfsky.cnblogs.com</i></p> <p> Last updated:<i>2010-10</i></p> <script>//Auxiliary Functions function WriteLine (info) {document.write (info + < /br> "); /************************************************** class and Namespace impersonation ************************************************** ////class definition function Class (info) {//private member var privatedata = "Private Data"; var privatemethod = function () {WriteLine ("Private"); function PrivateMethod2 (Info) {WriteLine ("Private");} Public members (use this). data = "public data"; This. method = function () {WriteLine (info); }; Static member of Class Class.staticdata = "static data"; Class.staticmethod = function (info) {WriteLine (info); namespace function NameSpace () {}/************************************************** target: New Namespace.class ("Hello Worl D "). Method (); ////Use the existing class definition, and hang it to the NameSpace under the static method Namespace.class1 = class; New Namespace.class1 ("New Namespace.class1 ()." Method ()). Method (); Or: New class definition Namespace.class2 = function (info) {this. method = function () {WriteLine (info); }; New Namespace.class2 ("New Namespace.class2 ()." Method ()). Method (); /************************************************** goal: NameSpace.Class.StaticMethod (); ///Static object + static method Namespace.class3 = {}; {} indicates that this is an object, or with the new object (); NameSpace.Class3.Method = function (info) {WriteLine (info); NameSpace.Class3.Method ("NameSpace.Class3.Method ()"); Or: New an object gives static member Namespace.class4 = new Class ("NameSpace.Class4.Method ()"); NameSpace.Class4.Method (); Alternatively, the anonymous function is used to define the class and then create the object NAMESPACE.CLASS5 = new (function (info) {this) with new. method = function () {WriteLine (info); }) ("NameSpace.Class5.Method ()"); NameSpace.Class5.Method (); Or: The JSON approach (class definition + create at the same time)//The advantage is simple, the disadvantage is notCan pass parameters in namespace.class6 = {method:function (info) {WriteLine (info);} }; NameSpace.Class6.Method ("NameSpace.Class6.Method ()"); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Ps. Very happy, vs2010 to JS IntelliSense more and more easy to use: P
Source: http://surfsky.cnblogs.com

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.