This keyword in JavaScript introduces and uses an instance _javascript tips

Source: Internet
Author: User

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.

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.