每一种语言都有变量的概念,变量是用来存储信息的一个元素。比如下面这个函数:

 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}

   Student类有三个变量,分别为name(名字),age(年龄),from(籍贯),这三个变量构成了描述一个对象的信息。当然,这里还有一个方法用来返回Student的信息。
   但是,我们是不是定义了一个变量,它就能一直存在着,并且还有可能在任何地方都能被访问和使用直到其被销毁?仔细想想,上面的需求是比较过分的,因为某些变量在某个功能实现后就不再利用了,但如果这个变量还存在的话,就占用了系统资源了,俗语曰:“站着茅坑不拉#$%”。
   于是我们对变量的及时和按需求地销毁有一个探讨的话题了。
   好,切入正题吧,就本人所接触过的来讲,js中支持如下几种类型的变量,分别为:局部变量、类变量、私有变量、实例变量、静态变量和全局变量。接下来我们就一一探讨研究下。

局部变量

局部变量一般指在{}范围内有效变量,也就是语句块内有效的变量,如:

 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+"<br>");
13 return sum;
14}

15 // document.write("sum is :" +sum+"<br>");
16 document.write( " result is : " + foo( true ) + " <br> " );

   该代码执行后输出的结果为:“index is :undefined” 和 “result is :0”,我们可以看到希望输出的index变量的值为undefined,也就是未定义。因此我们可以发现,index变量在if语句块结束后即被销毁了。那么“sum”变量呢?这个变量在foo()函数段执行完毕后被销毁了,如果您去掉我注释的那条语句,再执行,您将会发现系统将报错。值得注意的是,如果我把上面的foo()函数改成如下:

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


   您将可以看见可以输出index值("index is :10"),这个是js和其他语言的不同地方,因为index是在for循环的{}外面定义的,因此其作用范围在foo()函数使用完毕后才销毁。

  类变量:
   类变量,实际上就是类的一个属性或字段或一个方法,该变量在该类的一个实例对象被销毁后自动销毁,比如我们开始时举的Student类。这个我们不多讨论,大家可以自己试一下。

私有变量
   私有变量,值得是某个类自己内部是用的一个属性,外部无法调用,其定义是用 var 来声明的。注意如果不用var 来声明,该变量将是全局变量(我们下面将会讨论),如:

 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

   这里,我们分别定义了一个origName和FormatIt()两个私有变量(按面向对象的解释,应该用类的属性来称呼)。
   我们把这种情况下的方法也成为变量,因为该情况下的变量是个function类型的变量,而function也属于Object类的继承类。在这种情形下,如果我们定义了 var zfp = new Student("3zfp",100,"ShenZhen")。但无法通过zfp.origName和zfp.FormatIt()方式来访问这两个变量的。

注意以下几点

1、私有变量是不能用this来指示的。
2、私有方法类型的变量的调用必须是在该方法声明后。如我们将Student类改造如下:

 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}
16 var  zfp  =   new  Student( " 3zfp " , 100 , " ShenZhen " );

代码执行后,将会报"找不到对象"的错误.意思是FormatName()未定义。

3、私有方法无法访问this指示的变量(公开变量),如下:

 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}

19 var  zfp  =   new  Student( new  BasicInfo( " 3zfp " , 100 , " ShenZhen " ));
20
21

执行代码后,系统将会提示 “this.basicInfo为空或不是对象”的错误。
基本结论是,私有方法只能访问私有属性,私有属性在声明并赋值后可以在类的任何地方访问

实例变量:
实例变量即某个实例对象所拥有的变量。如:

 1
 2 function  BasicInfo(name,age,from)
 3 {
 4 this.name = name;
 5 this.age = age;
 6 this.from = from;
 7}

 8 var  basicA  =   new  BasicInfo( " 3zfp " , 100 , " ShenZhen " );
 9 basicA.generalInfo  =   " is 3zfp owned object " ;
10 document.write( " basicA's generalInfo is :  " +  basicA.generalInfo + " <br> " );
11 var  basicB  =   new  BasicInfo( " zfp " , 100 , " ShenZhen " );
12 document.write( " basicB's generalInfo is :  " +  basicB.generalInfo + " <br> " );
13 执行该代码后,我们将可以看到如下结果:
14 basicA's generalInfo is : is 3zfp owned object
15 basicB's generalInfo is : undefined
16

静态变量:

静态变量即为某个类所拥有的属性,通过 类名+"."+静态变量名 的方式访问该属性。如下可以做清晰的解释:

 1 function  BasicInfo(name,age,from)
 2 {
 3 this.name = name;
 4 this.age = age;
 5 this.from = from;
 6}

 7 BasicInfo.generalInfo  =   " is 3zfp owned object " ;
 8 var  basic  =   new  BasicInfo(