Js variable scope and accessibility _ Javascript tutorial

Source: Internet
Author: User
JavaScript: exploring the scope and accessibility of js variables. Javascript tutorial

Every language has the concept of variables, which are an element used to store information. For example, the following function:

1 function Student (name, age, from)
2 {
3 this. name = name;
4 this. age = age;
5 this. from = from;
6 this. ToString = function ()
7 {
8 return "my information is name:" + this. name + ", age:" + this. age + ", from:" + this. from;
9}
10}
The Student class has three variables: name, age, and from. These variables constitute information about an object. Of course, there is another way to return Student information.
However, do we define a variable that can always exist and may be accessible and used anywhere until it is destroyed? Think about it, the above requirement is too much, because some variables are no longer used after a function is implemented, but if this variable still exists, it will occupy system resources, as the saying goes: "standing in the moukeng without pulling # $ % ".
So we have a topic about timely and on-demand destruction of variables.
Well, let's get to the point. As far as I have been concerned, js supports the following types of variables: local variables, class variables, private variables, instance variables, static variables, and global variables. Next we will discuss and study it one by one.

Local variables:

Local variables generally refer to valid variables in the range of {}, that is, valid variables in the statement block, such:

1 function foo (flag)
2 {
3 var sum = 0;
4 if (flag = true)
5 {
6 var index;
7 for (index = 0; index <10; index ++)
8 {
9 sum + = index;
10}
11}
12 document. write ("index is:" + index +"
");
13 return sum;
14}
15 // document. write ("sum is:" + sum +"
");
16document. write ("result is:" + foo (true) +"
");
After the code is executed, the output results are "index is: undefined" and "result is: 0". We can see that the value of the index variable to be output is undefined, that is, undefined. Therefore, we can find that the index variable is destroyed after the if statement block is complete. What about the "sum" variable? This variable is destroyed after the foo () function segment is executed. if you remove the statement I commented on and execute it again, the system reports an error. It is worth noting that if I change the above foo () function to the following:

1 function foo (flag)
2 {
3 var sum = 0;
4 for (var index = 0; index <10; index ++)
5 {
6 sum + = index;
7}
8 document. write ("index is:" + index +"
");
9 return sum;
10}

You can see that the output index value ("index is: 10") is different from other languages in js, because the index is defined outside the {} of the for loop, therefore, the function is destroyed only after the foo () function is used.

Class variables:
A class variable is actually an attribute, field, or method of a class. This variable is automatically destroyed after an instance object of this class is destroyed, such as the Student class we started. We will not discuss this much. You can try it on your own.

Private variable:
Private variables are an internal attribute of a class and cannot be called outside. Their definitions are declared using var. Note that if you do not use var to declare the variable, it will be a global variable (we will discuss it below), such:

1 function Student (name, age, from)
2 {
3
4 this. name = FormatIt (name );
5 this. age = age;
6 this. from = from;
7 var origName = name;
8 var FormatIt = function (name)
9 {
10 return name. substr (0, 5 );
11}
12 this. ToString = function ()
13 {
14 return "my information is name:" + origName + ", age:" + this. age + ", from:" + this. from;
15}
16}
17
18
Here, we define a private variable origName and FormatIt () respectively (according to object-oriented interpretation, class attributes should be called ).
In this case, the method also becomes a variable, because the variable in this case is a function type variable, and the function also belongs to the inheritance class of the Object class. In this case, if we define var zfp = new Student ("3zfp", 100, "ShenZhen "). However, zfp. origName and zfp. FormatIt () cannot be used to access these two variables.

Note the following:

1. Private variables cannot be indicated by this.
2. variables of the private method type must be called after the method is declared. For example, we transform the Student class as follows:

1 function Student (name, age, from)
2 {
3 var origName = name;
4 this. name = FormatName (name );
5 this. age = age;
6 this. from = from;
7 var FormatName = function (name)
8 {
9 return name + ". china ";
10}
11 this. ToString = function ()
12 {
13 return "my information is name:" + origName + ", age:" + this. age + ", from:" + this. from;
14}
15}
16var zfp = new Student ("3zfp", 100, "ShenZhen ");
After the code is executed, an error "the object cannot be found" is reported, indicating that FormatName () is not defined.

3. Private methods cannot access the variables indicated by this (public variables), as shown below:

1
2 function Student (basicinfo)
3 {
4 this. basicInfo = basicinfo;
5
6 var FormatInfo = function ()
7 {
8 this. basicInfo. name = this. basicInfo. name + ". china ";
9}
10 FormatInfo ();
11
12}
13 function BasicInfo (name, age, from)
14 {
15 this. name = name;
16 this. age = age;
17 this. from = from;
18}
19var zfp = new Student (new BasicInfo ("3zfp", 100, "ShenZhen "));
20
21
After the code is executed, the system will prompt the error "this. basicInfo is empty or not an object.
The basic conclusion is that private methods can only access private attributes. Private attributes can be accessed anywhere in the class after they are declared and assigned values,

[1] [2] Next page

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.