Discussion on the scope and accessibility of JS variable

Source: Internet
Author: User
Tags define definition execution variables variable tostring valid access
js| Variable | access

Each language has the concept of a variable, which is an element used to store information. For example, the following function:

1function Student (Name,age,from)
2{
3 this.name = name;
4 This.age = age;
5 This.from = from;
6 this. ToString = function ()
7 {
8 return "I Information is Name:" +this.name+ ", Age:" +this.age+ ", From:" +this.from;
3 ·
10}
The student class has three variables, named (first name), Age (Ages), from (native), and these three variables form the information that describes an object. Of course, there is also a way to return student information.
But do we define a variable that can exist all the time and can be accessed and used anywhere until it is destroyed? Think carefully, the above demand is more excessive, because some of the variables in a function is no longer used, but if this variable still exists, it occupies the system resources, the saying goes: "Standing in the manger does not pull #$%."
So we have a discussion about the timeliness of the variables and the need to destroy them.
OK, cut to the point, in my contact, JS support for the following types of variables, respectively: Local variables, class variables, private variables, instance variables, static variables and global variables. Next we'll look at the one by one study.

Local variables:

A local variable generally refers to a valid variable within the scope of {}, which is a valid variable within the statement block, such as:

1function 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}
document.write ("Index is:" +index+ "<br>");
return sum;
14}
15//document.write ("Sum is:" +sum+ "<br>");
16document.write ("result is:" +foo (True) + "<br>");
The output of the code after the execution is: "Index is:undefined" and "result is:0", we can see that the value of the index variable you want to output is undefined, which is undefined. So we can see that the index variable is destroyed when the block of the IF statement is finished. What about the "sum" variable? This variable was destroyed after execution of the Foo () function segment, and if you remove the statement that I commented on and then execute, you will find that the system will make an error. It is worth noting that if I change the Foo () function above to read:

1function 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+ "<br>");
9 return sum;
10}

You will see that you can output the index value ("Index Is:10"), which is different in JS and other languages, because index is defined outside of the For loop {}, so its scope is destroyed after the Foo () function has been used.

Class variables:
A class variable, in fact, is a property or field of a class or a method that is automatically destroyed after an instance object of that class is destroyed, such as the student class we lifted at the beginning. We don't have much discussion about this, we can try it by ourselves.

Private variable:
A private variable, worth being a property of a class that is internally used, cannot be invoked externally, and its definition is declared with var. Note If you do not declare VAR, the variable will be a global variable (as we'll discuss below), such as:

1function 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 {
Ten return Name.substr (0,5);
11}
this. ToString = function ()
13 {
Return ' My information is name: ' +origname+ ', Age: ' +this.age+ ', from: +this.from;
15}
16}
17
18
Here, we define a origname and Formatit () two private variables (which, by object-oriented interpretation, should be called by the properties of the Class).
We also make a variable in this case because the variable in that case is a variable of the function type, and the function belongs to the inheriting class of the object class. In this case, if we define the var ZFP = new Student ("3ZFP", "Shenzhen"). However, these two variables cannot be accessed through the Zfp.origname and Zfp.formatit () methods.

Note the following points:

1. Private variables cannot be indicated with this.
2. The invocation of a variable of a private method type must be after the method declaration. If we transform the student class into the following:

1function 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+ "."
10}
One by one. ToString = function ()
12 {
"I Information is Name:" +origname+ ", Age:" +this.age+ ", From:" +this.from;
14}
15}
16var ZFP = new Student ("3ZFP", "M", "Shenzhen");
After the code executes, the error "object cannot be found" is reported. The meaning of FormatName () is undefined.

3. The private method cannot access the variable that is indicated by this (expose variable) as follows:

1
2function Student (Basicinfo)
3{
4 This.basicinfo = Basicinfo;
5
6 var formatinfo = function ()
7 {
8 This.basicInfo.name = this.basicinfo.name+ ".";
3 ·
Ten FormatInfo ();
11
12}
13function Basicinfo (Name,age,from)
24X
THIS.name = name;
This.age = age;
This.from = from;
18}
19var ZFP = new Student (New Basicinfo ("3ZFP", "Shenzhen"));
20
21st
After the code is executed, the system will prompt for an error "This.basicinfo is empty or is not an object."
The basic conclusion is that private methods can only access private properties, and private properties are declared and assigned to be accessible anywhere in the class.

Instance variables:
An instance variable is a variable owned by an instance object. Such as:

1
2function Basicinfo (Name,age,from)
3{
4 this.name = name;
5 This.age = age;
6 this.from = from;
7}
8var Basica = new Basicinfo ("3ZFP", "M", "Shenzhen");
9basica.generalinfo = "is 3ZFP owned object";
10document.write ("Basica ' s generalinfo is:" + basica.generalinfo+ "<br>");
11var BASICB = new Basicinfo ("ZFP", "Shenzhen");
12document.write ("BASICB ' s generalinfo is:" + basicb.generalinfo+ "<br>");
13 After executing the code, we will see the following results:
14basicA ' s GeneralInfo is:is 3zfp owned Object
15basicB ' s GeneralInfo is:undefined
16
Static variables:

A static variable is a property owned by a class, with the class name + "." + static variable name to access this property. The following can be clearly explained:

1function Basicinfo (Name,age,from)
2{
3 this.name = name;
4 This.age = age;
5 This.from = from;
6}
7basicinfo.generalinfo = "is 3ZFP owned object";
8var basic = new Basicinfo ("ZFP", "Shenzhen");
9document.write (basic.generalinfo+ "<br>");
10document.write (basicinfo.generalinfo+ "<br>");
11basicinfo.generalinfo = "info is changed";
12document.write (basicinfo.generalinfo+ "<br>");
By executing the above code, you will get the following results:
Undefined
is 3ZFP owned object
Info is changed

Note the following points:
1, the class name + "." + static variable name method to declare a static variable
2. A static variable is not a property unique to an instance object of a class and is shared by an object.
3, can be the instance object name + "." + static variable name to access.

Global variables:
A global variable, which is a variable that effectively accesses control during the entire system run, is usually defined at the beginning of a JS code, such as:

1
2var copyright = "3ZFP owned";
3var foo =function ()
4{
5 window.alert (copyright);
6}
Note the following points:

1. If a variable is declared without Var, it is considered a global variable. Such as:

1var copyright = "3ZFP owned";
2var foo =function (fooinfo)
3{
4 _foo = Fooinfo;
5 document.write (copyright+ "<br>");
6}
7new foo ("foo test");
8document.write (_foo+ "<br>");
Executing the code, you get the following results:
3ZFP Owned
Foo test
But here's another note, the function is the compile-time object, which means that the _foo global variable is initialized after the Foo object is instantiated, which means that if the
New Foo ();
document.write (_foo+ "<br>");
Swap into
document.write (_foo+ "<br>");
New Foo ();
The system prompts "_foo undefined".
2. If you define a local variable attribute with the same name as a global variable, the following:

1
2
3var copyright = "3ZFP owned";
4var foo =function (fooinfo)
5{
6 var copyright = Fooinfo; Variable with the same name
7 This.showinfo = function ()
8 {
9 document.write (copyright+ "<br>");
10}
11}
12new foo ("foo test"). Showinfo ();
13document.write (copyright+ "<br>");
Executing the code, you get the following results:
3ZFP Owned
Foo test

The reason is that the function is the definition of the variable that was completed during compilation, that is, the definition of the copyright within Foo was completed during compilation, and its scope was valid only within the Foo object, regardless of the externally defined global variable, copyright.



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.