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