Simulation of JavaScript classes and namespaces

Source: Internet
Author: User
Document directory
  • 1. Simulation of Classes
  • 2. namespace Simulation
  • 4. Objective: To Call static functions of a class

Let's just repeat the wheel and sort out this document for people in need, including me :)

First, the simplest one is:

// The following lines of code demonstrate the definition and usage of namespaces, classes, and functions:
Namespace = {};
Namespace. Class = function (){
This. method = function (Info) {alert (Info );}
};
New namespace. Class (). Method ("Hello World ");

 

 

Some more visible code in various situations

1. Simulation of Classes // Class definition
Function class (Info ){
// Private member
VaR privatedata = "private data ";
VaR privatemethod = function () {writeline ("private ");};
Function privatemethod2 (Info) {writeline ("private ");}

// Public Member (use this)
This. Data = "Public Data ";
This. method = function () {writeline (Info );};
};

// Static member of the class
Class. staticdata = "static data ";
Class. staticmethod = function (Info) {writeline (Info );};

 

 

2. namespace Simulation

Function namespace (){}
Or
Namespace = {};
Or
Namespace = new object ();

 


3. Goal: to create a class instance and call the instance method

VaR o = new namespace. Class ("Hello World ");

O. Method ();

// Use existing class definitions and use static methods to mount them to the namespace
Namespace. class1 = Class;
New namespace. class1 ("New namespace. class1 (). Method ()"). Method ();

// Or: Create a class definition
Namespace. class2 = function (Info ){
This. method = function () {writeline (Info );};
};
New namespace. class2 ("New namespace. class2 (). Method ()"). Method ();

 

 

4. Objective: To Call static functions of a class

Namespace. Class. staticmethod ();

// Static object + static method
Namespace. class3 ={}; // {} indicates that this is an object, or use new object ();
Namespace. class3.method = function (Info) {writeline (Info );};
Namespace. class3.method ("namespace. class3.method ()");

// Or: assign a new object to a static member.
Namespace. class4 = new class ("namespace. class4.method ()");
Namespace. class4.method ();

// Or: anonymous functions are used to define classes, and new objects are used to create objects.
Namespace. class5 = new (function (Info ){
This. method = function () {writeline (Info );};
}) ("Namespace. class5.method ()");
Namespace. class5.method ();

// Or: JSON (class definition + creation completed at the same time)
// The advantage is that the parameter cannot be passed in.
Namespace. class6 = {
Method: function (Info) {writeline (Info );}
};
Namespace. class6.method ("namespace. class6.method ()");

 

Download Code:/files/surfsky/oo.htm

PS. I'm very happy that vs2010's smart perception of JS is getting better and better: P

 

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.