Home > Web Front-end > JS Tutorial > body text

Introduction and usage examples of this keyword in JavaScript_javascript skills

WBOY
Release: 2016-05-16 17:31:58
Original
989 people have browsed it

When creating a class, if you want each new class below to have some common variables or other functions, the 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 to illustrate the issue of access permissions of this class.

Copy code The code is as follows:

//Person class
function Person(){
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 a public variable, which can be accessed externally

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(){//shouw2 method is a private method inside the 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 declares private variables inside the class and cannot be accessed externally
var age = 20;

this.name2="edg ";//This declares a public variable that can be accessed externally

this.show=function(){//The shou method is a public method that can be accessed externally and can access private methods within 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 , 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

Ask, the variables declared through this keyword are public variables and can be accessed externally. Of course, you only need to expose a method to achieve external access

Ask about the private variables inside the class. This.show=function(){} declares a public method, which can also be called outside the class. Of course, with this

By analogy, directly declared methods are private methods/

Let’s look at another example

Copy code The code is 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 that who calls this method, this refers to the object, for example, the window object calls test method,

So inside the test method, this, v refers to whether a v variable is defined in the window, that is, externally. You can know by looking at it that

is defined

A var v = 902; so what this method calls is actually the value of v.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!