Home  >  Article  >  Web Front-end  >  Introduction to this keyword in JavaScript

Introduction to this keyword in JavaScript

高洛峰
高洛峰Original
2016-11-26 09:55:151042browse

JavaScript is actually a language based on or object-oriented. In this case, the keyword this is particularly important inside a class. When

creates a class, if you want each new class below To have some common variables or other functions, this keyword is the best way.

Of course, since it is an object-oriented language, there must be access rights issues, which are also closely related to the this keyword. Let’s demonstrate an example below to illustrate the issue of access permissions for this class.

[javascript]

//Person class

function Person(){

var name="abc";//Everything declared in var It is a private variable inside the class and cannot be accessed by the outside.

var age = 20;

this.name2="edg";//This declares a public variable and can be accessed by the outside.

this.show=function(){// The shou method is a public method that can be accessed externally and can access private methods inside the class
  window.alert(name);
  }
   
  function show2(){//The shouw2 method is a private method inside the class and cannot be accessed externally Visit
                                                                                                                                                                   (){
var name="abc";//var declares private variables inside the class and cannot be accessed from the outside
var age = 20;

this.name2="edg";//this declares public variables Variables, externally accessible

this.show=function(){//The shou method is a public method, which can be accessed externally, and can access private methods inside the class
window.alert(name);
}

function show2 (){//The shouw2 method is a private method inside the class and cannot be accessed from the outside

}
}

var p1 = new Person();
document.writeln(p1.name2+p1.name);
p1. show();
This Person is actually a class, and the class name is Person. The variables declared in it, starting with var, are all private variables, which can only be accessed within the class, and the variables declared through the this keyword It is a public variable and can be accessed externally. Of course, you only need to expose a method to achieve external access to private variables inside the class. This.show=function(){} declares a public method that can also be called outside the class. Of course, by analogy, the directly declared method is a private method/

Let’s look at another example

[javascript]
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, what this means is, who called it This method, this this refers to the object, for example, the test method called by the window object,

So inside the test method, this this, v refers to whether a v variable is defined in the window, that is, the external global. You can check it by checking You know, a var v = 902 is defined

; so what this method calls is actually the value of v.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn