Javascript variable scope and accessibility _ javascript skills

Source: Internet
Author: User
The scope and accessibility of js variables are discussed in every language. variables are an element used to store information. For example, the following function:

The Code is 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: 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:

The Code is 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 +"
");
Return sum;
}
// Document. write ("sum is:" + sum +"
");
Document. 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:

The Code is as follows:

Function foo (flag)
{
Var sum = 0;
For (var index = 0; index <10; index ++)
{
Sum + = index;
}
Document. write ("index is:" + index +"
");
Return sum;
}


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:

The Code is 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 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:

The Code is 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 + ". china ";
}
This. ToString = function ()
{
Return "my information is name:" + origName + ", age:" + this. age + ", from:" + this. from;
}
}
Var 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:


The Code is as follows:

Function Student (basicinfo)
{
This. basicInfo = basicinfo;

Var FormatInfo = function ()
{
This. basicInfo. name = this. basicInfo. name + ". china ";
}
FormatInfo ();
}
Function BasicInfo (name, age, from)
{
This. name = name;
This. age = age;
This. from = from;
}
Var zfp = new Student (new BasicInfo ("3zfp", 100, "ShenZhen "));

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,

Instance variables:
Instance variables are the variables owned by an instance object. For example:

The Code is as follows:

Function BasicInfo (name, age, from)
{
This. name = name;
This. age = age;
This. from = from;
}
Var basicA = new BasicInfo ("3zfp", 100, "ShenZhen ");
BasicA. generalInfo = "is 3zfp owned object ";
Document. write ("basicA's generalInfo is:" + basicA. generalInfo +"
");
Var basicB = new BasicInfo ("zfp", 100, "ShenZhen ");
Document. write ("basicB's generalInfo is:" + basicB. generalInfo +"
");
After executing the code, we can see the following results:
BasicA's generalInfo is: is 3zfp owned object
BasicB's generalInfo is: undefined

Static variables:

Static variables are the attributes of a class. They are accessed by class name + "." + static variable name. The following is a clear explanation:

The Code is 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", 100, "ShenZhen ");
Document. write (basic. generalInfo +"
");
Document. write (BasicInfo. generalInfo +"
");
BasicInfo. generalInfo = "info is changed ";
Document. write (BasicInfo. generalInfo +"
");


Run the above Code and you will get the following results:
Undefined
Is 3zfp owned object
Info is changed

Note the following:
1. Declare a static variable by class name + "." + static variable name
2. Static variables do not belong to an instance object of the class and are shared by objects.
3. Access by Instance Object Name + "." + static variable name.

Global variables:
Global variables are the variables for effective access control during system operation. They are usually defined at the beginning of a javascript code, such:

The Code is as follows:

Var copyright = "3zfp owned ";
Var foo = function ()
{
Window. alert (copyright );
}

Note the following:
1. If a variable is declared without var, it is regarded as a global variable. For example:
Var copyright = "3zfp owned ";
Var foo = function (fooInfo)
{
_ Foo = fooInfo;
Document. write (copyright +"
");
}
New foo ("foo test ");
Document. write (_ foo +"
");
Run the following code:
3zfp owned
Foo test
However, there is another note that function is a compilation object, that is, the global variable _ foo must be initialized after the foo object is instantiated, that is, if
New foo ();
Document. write (_ foo +"
");
Reconciliation
Document. write (_ foo +"
");
New foo ();
The system prompts "_ foo undefined ".
2. If you define a local variable attribute with the same name as the global variable, as follows:

The Code is as follows:

Var copyright = "3zfp owned ";
Var foo = function (fooInfo)
{
Var copyright = fooInfo; // variable with the same name
This. showInfo = function ()
{
Document. write (copyright +"
");
}
}
New foo ("foo test"). showInfo ();
Document. write (copyright +"
");

Run the following code:
3zfp owned
Foo test

The reason is that function defines variables during compilation, that is, the copyright definition in foo is completed during compilation, and its scope is only valid within the foo object, it is not related to the external definition of the 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.