This keyword is the best way to create a class when you want each of the following new classes to have some generic variables or other functions.
Of course, since it is an object-oriented language, there must be a problem with access rights, and this is also closely related to this keyword. Let's show you an example to illustrate the problem of access rights for this class.
Copy Code code as follows:
Person class
function person () {
var name= "abc";//var declarations are private variables inside a class that cannot be accessed externally
var age = 20;
This.name2= "EDG";//this declares a public variable that can be accessed externally
This.show=function () {//shou method is a public method that can be accessed externally and can access private methods within the class
Window.alert (name);
}
function Show2 () {//shouw2 method is a private method inside a class and cannot be accessed externally
}
}
var p1 = new Person ();
Document.writeln (P1.name2+p1.name);
P1.show ();
Person Class Www.jb51.net
function person () {
var name= "abc";//var declarations are private variables inside a class that cannot be accessed externally
var age = 20;
This.name2= "EDG";//this declares a public variable that can be accessed externally
This.show=function () {//shou method is a public method that can be accessed externally and can access private methods within the class
Window.alert (name);
}
function Show2 () {//shouw2 method is a private method inside a class and cannot be accessed externally
}
}
var p1 = new Person ();
Document.writeln (P1.name2+p1.name);
P1.show ();
This person is actually a class, the class name is the man, in which the declared variable, VAR begins with a private variable, can only be accessed within the class
Ask, and the variables declared through this keyword are public variables that can be accessed externally. Of course, the external interview can be achieved only by exposing one method
Ask the private variable inside the class, and this this.show=function () {} is declaring a common method that can be invoked outside of the class. Of course, in this
By analogy, the direct declaration method is a private method
Let's look at one more example
Copy Code code as follows:
function Test () {
alert (THIS.V);
}
var v = 902;
Window.test ();
function Test () {
alert (THIS.V);
}
var v = 902;
Window.test ();
The code is very short, this means, who calls this method, this is the object that refers to, for example, the Window object calls the test method,
So inside the test method, this this,v is the definition of a V variable in window, the external global, by looking at the definition of
a var v = 902, so this method is actually called the value of V.