Discussion on scope and accessibility of JS variable _javascript tips

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

Copy Code code as follows:
function Student (name,age,from)
{
THIS.name = name;
This.age = age;
This.from = from;
This. ToString = function ()
{
Return ' My information is name: ' +this.name+ ', Age: ' +this.age+ ', from: +this.from;
}
}

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:

Copy Code code as follows:
function foo (flag)
{
var sum = 0;
if (flag = = True)
{
var index;
for (index=0;index<10;index++)
{
Sum +=index;
}
}
document.write ("Index is:" +index+ "<br>");
return sum;
}
document.write ("Sum is:" +sum+ "<br>");
document.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:

Copy Code code as follows:
function foo (flag)
{
var sum = 0;
for (Var index=0;index<10;index++)
{
Sum +=index;
}
document.write ("Index is:" +index+ "<br>");
return sum;
}

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:

Copy Code code as follows:
function Student (name,age,from)
{

THIS.name = formatit (name);
This.age = age;
This.from = from;
var origname = name;
var formatit = function (name)
{
Return Name.substr (0,5);
}
This. ToString = function ()
{
Return ' My information is name: ' +origname+ ', Age: ' +this.age+ ', from: +this.from;
}
}

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:

Copy Code code as follows:
function Student (name,age,from)
{
var origname = name;
THIS.name = FormatName (name);
This.age = age;
This.from = from;
var formatname = function (name)
{
Return name+ "."
}
This. ToString = function ()
{
Return ' My information is name: ' +origname+ ', Age: ' +this.age+ ', from: +this.from;
}
}
var zfp = new Student ("3ZFP", "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:


Copy Code code as follows:
function Student (basicinfo)
{
This.basicinfo = Basicinfo;

var formatinfo = function ()
{
This.basicInfo.name = this.basicinfo.name+ ".";
}
FormatInfo ();
}
function Basicinfo (name,age,from)
{
THIS.name = name;
This.age = age;
This.from = from;
}
var zfp = new Student (New Basicinfo ("3ZFP", "Shenzhen"));
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:
Copy Code code as follows:
function Basicinfo (name,age,from)
{
THIS.name = name;
This.age = age;
This.from = from;
}
var basica = new Basicinfo ("3ZFP", "Shenzhen");
Basica.generalinfo = "is 3ZFP owned object";
document.write ("Basica ' s generalinfo is:" + basica.generalinfo+ "<br>");
var BASICB = new Basicinfo ("ZFP", "Shenzhen");
document.write ("BASICB ' s generalinfo is:" + basicb.generalinfo+ "<br>");
After executing the code, we will see the following results:
Basica ' s GeneralInfo is:is 3zfp owned Object
BASICB ' s GeneralInfo is:undefined
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:

Copy Code code as follows:
function Basicinfo (name,age,from)
{
THIS.name = name;
This.age = age;
This.from = from;
}
Basicinfo.generalinfo = "is 3ZFP owned object";
var basic = new Basicinfo ("ZFP", "Shenzhen");
document.write (basic.generalinfo+ "<br>");
document.write (basicinfo.generalinfo+ "<br>");
Basicinfo.generalinfo = "info is changed";
document.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:
Copy Code code as follows:
var copyright = "3ZFP owned";
var foo =function ()
{
Window.alert (copyright);
}
Note the following points:
1. If a variable is declared without Var, it is considered a global variable. Such as:
var copyright = "3ZFP owned";
var foo =function (Fooinfo)
{
_foo = Fooinfo;
document.write (copyright+ "<br>");
}
New Foo ("foo test");
document.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:
Copy Code code as follows:
var copyright = "3ZFP owned";
var foo =function (Fooinfo)
{
var copyright = Fooinfo; Variable with the same name
This.showinfo = function ()
{
document.write (copyright+ "<br>");
}
}
New Foo ("foo test"). Showinfo ();
document.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.