It is often seen in several groups that someone asks how a function in a class calls this. The exposed method after definition. Now I am publishing an article on implementation of the class. First, let's talk about the class. In a class, we will have the following features: 1. Public method 2. Private method 3. Attribute 4. Private... SyntaxHighlighter. all ();
It is often seen in several groups that someone asks how a function in a class calls this. The exposed method after definition. Now I am publishing an article on implementation of the class.
First, let's talk about the class. In a class, we have the following features:
1. Public Method
2. Private Method
3. Attributes
4. Private Variables
5. Constructor
Let's look at an example:
/*** Define class ***/
Var Class = function (){
Var _ self = this; // reference a negative value to a variable.
Var _ Field = "Test Field"; // Private Field
Var privateMethod = function () {// Private Method
Alert (_ self. Property); // call the Property
}
This. Property = "Test Property"; // public Property
This. Method = function () {// public Method
Alert (_ Field); // call a private Field
PrivateMethod (); // call the private Method
}
}
I have written all the comments here, and you will probably understand them at a glance. For friends who write less JavaScript code, they may wonder why I define a variable named _ self, because in JS, this does not need to be used in other object languages, this will change in the parsing and running processes. Here is a brief introduction to the definition of this in js. If you need this, I can open one more article.
Definition: this is the object to which the function that contains it is called as a method.
Feature: the environment of this can be changed as the function is assigned to different objects!
If you are interested, you can find information on the Internet to find out the correct answer. Here _ self aims to open a private variable and direct the reference to the class itself. Www.2cto.com
I have just mentioned a constructor problem, which can be implemented directly using code. At the end of the function, write and execute the Code directly.
/*** Define class ***/
Var Class = function (){
Var _ self = this; // reference a negative value to a variable.
Var _ Field = "Test Field"; // Private Field
Var privateMethod = function () {// Private Method
Alert (_ self. Property); // call the Property
}
This. Property = "Test Property"; // public Property
This. Method = function () {// public Method
Alert (_ Field); // call a private Field
PrivateMethod (); // call the private Method
}
/*** Constructor ***/
Var init = function (){
PrivateMethod ();
}
Init ();
}
Use this class
Var c = new Class ();
C. Method (); // usage
So OK.
From the heart and white