This is a keyword in the Javascript language. The value of this will change as the function is used. But there is a general principle, that is, this refers to the object that calls the function. This pointer in JavaScript is a dynamic variable, and this pointer in a method does not always point to the object that defines the method. This keyword in Javascript usually points to the owner of the current function.
Let me introduce it to you through the code below. The specific code is as follows:
<script type="text/javascript">function Person(){/*The attribute defined using the var attribute name is a private property of the class and cannot be accessed by the outside world. If you want to be accessed by the outside world, you must use the public method*/var QQ=""; var Email="@qq.com";/*The attribute defined by this. attribute name is the public attribute of the class, which is accessible by the outside world*/this.Name="光电影";this.Age=;/*Define the public method of the Person class. All public attributes and public methods of the class must be used to define this*/this.Show=function(){//Define the public method of the Person class (privileged method) alert("QQ="+QQ+"/tEmail="+Email);}/*Define the private method of the Person class*/function Show(){//This is a private method of the Person class, which can only be used inside the Person class}}var p = new Person();alert("Private attribute p.QQ="+p.QQ+"/t"+"p.Email="+p.Email);alert("Public attribute p.Name="+p.Name+"/t"+"p.Age="+p.Age);p.Show();//p.Show();//p.Show();//An error will be reported here, and the private method of the Person class cannot be called. /*Window object description Window object represents a browser window or a framework. In client JavaScript, Window objects are global objects, and all expressions are evaluated in the current environment. In other words, it does not require special syntax to reference the current window, and you can use the properties of that window as a global variable. For example, you can just write a document without having to write a window.document. Similarly, the method of the current window object can be used as a function, such as just writing alert() instead of having to write Window.alert(). */function Test(){alert(this.v);}v=;Test();window.Test();//Remember one sentence: Which object calls the function where this is located, then this represents which object instance. Here is the window object calling the Test() method, so this refers to the window object function Test(){this.fnTest=function(){alert(this.v);}}var t = new Test();tv="Remember one sentence: Which object calls the function where this is located, then this represents which object instance";t.fnTest();//Here is the t object calling the Test() method, so this refers to the t object at this time</script>This summary of JavaScript:
1. This cannot be used outside the class definition, but can only be used inside the class definition.
2. Which object calls the function where this is located, then this represents which object instance.
I will introduce this to you here to summarize the JavaScript knowledge points (10). I hope it will be helpful to you!