JavaScript Knowledge Point Summary (10) of this keyword _javascript tips

Source: Internet
Author: User
Tags class definition

This is a keyword in the JavaScript language. The value of this will change as the function is used differently. But there is a general principle, and that is this refers to the object that invokes the function. The this pointer in JavaScript is a dynamic variable, and the this pointer within a method does not always point to the object that defines the method. The This keyword in JavaScript usually points to the owner of the current function.

The following through the code to introduce you. The specific code looks like this:

<script type= "Text/javascript" > Function person () {/* the attribute defined using the var attribute name is a private property of the class that cannot be accessed, and to be accessed by the outside world, it is necessary to use the public method */var qq= "";
var email= "@qq. com"; /* Use this. The property defined by the property name is the public property of the class and is accessible by the outside * *.
Name= "Aloof Wolf"; This.
age=; /* Defines the public method for the person class, which is used by this*/this to define the public properties and public methods of the class. Show=function () {///defines the public method (privileged method) alert ("qq=" +qq+ "\temail=" +email) of the Person class,}/* Defines the private method of the person class */function Show () {//
This is a private method of the person class that can only be used inside the person class with the} var p = new person (); Alert (private property P.)
qq= "+p.qq+" "T" + "p.email=" +p.email); Alert ("Public property P.")
Name= "+p.name+" "T" + "p.age=" +p.age);
P.show (); P.show ();//This will be an error, the private method of the person class cannot be invoked, the/* Window Object Description Window object represents a browser window or a frame.
In client JavaScript, the Window object is a global object and all expressions are evaluated in the current environment. In other words, to refer to the current window does not require a special syntax, you can use the properties of that window as a global variable.
For example, you can write only the document without having to write window.document.
Similarly, you can use the method of the current Window object as a function, such as write only alert () without having to write Window.alert ().
*/function Test () {alert (THIS.V);} v=;
Test (); Window.
Test ()//Remember one word: Which object calls this function, so this represents which object instance, where the Window object invokes the Test () method, so this means that this is the Window object function test () { This.fntest=function () {alert (this.v);
} var t = new Test ();
T.v= "Remember a word: Which object calls this function, then this represents which object instance";  T.fntest ()///This is the T object calling the test () method, so this is the T object at this point </script>

This summary of javascript:

1. This cannot be used outside of the class definition and can only be used within the class definition.

2, which object calls this function, then this represents which object instance.

About JavaScript Knowledge Point Summary (10) of this keyword understanding to everyone here, I hope to help!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.