Home  >  Article  >  Web Front-end  >  Discuss some techniques in using JavaScript variable types

Discuss some techniques in using JavaScript variable types

伊谢尔伦
伊谢尔伦Original
2017-07-18 10:41:541565browse

js supports the following types of variables: local variables, class variables, private variables, instance variables, static variables and global variables.

Local variables:
Local variables generally refer to variables that are valid within the scope of {}, that is, variables that are valid within the statement block, such as:

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>");

This code The output results after execution are: "index is :undefined" and "result is :0". We can see that the value of the index variable we want to output is undefined, that is, undefined. Therefore, we can find that the index variable is destroyed after the if statement block ends. What about the "sum" variable? This variable is destroyed after the foo() function section is executed. If you remove the statement I commented and execute it again, you will find that the system will report an error. It is worth noting that if I change the foo() function above to the following:

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 be able to see that the index value ("index is :10") can be output. This is js and other Different places in the language, because index is defined outside the {} of the for loop, its scope is not destroyed until the foo() function is used.

Class variable:
Class variable is actually an attribute or field or a method of the class. This variable is automatically destroyed after an instance object of the class is destroyed, such as the Student we mentioned at the beginning kind. We won’t discuss this much, you can try it yourself.

Private variable:
Private variable is an attribute used internally by a class and cannot be called externally. Its definition is declared using var. Note that if you do not declare it with var, the variable will be a global variable (we will discuss it below), such as:

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 two private variables, one origName and FormatIt() respectively ( According to the object-oriented interpretation, it should be called by the attributes of the class).
We also call the method in this case a variable, because the variable in this case is a function type variable, and function also belongs to the inheritance class of the Object class. In this case, if we define var zfp = new Student("3zfp",100,"ShenZhen"). But these two variables cannot be accessed through zfp.origName and zfp.FormatIt().

Note the following points:

1. Private variables cannot be indicated by this.
2. The call to a variable of private method type must be made after the method is declared. For example, we transform the Student class 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 "object not found" error will be reported. This means that FormatName() is not defined.

3. Private methods cannot access the variable (public variable) indicated by this, 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 executing the code, the system will prompt "this.basicInfo is empty or not an object "mistake.
The basic conclusion is that private methods can only access private properties. Private properties can be accessed anywhere in the class after being declared and assigned.

Static variables:
Static variables are owned by a class Owned attributes, access this attribute through class name + "." + static variable name. The following can be explained clearly:

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+"<br>");  
document.write(BasicInfo.generalInfo+"<br>");  
BasicInfo.generalInfo = "info is changed";  
document.write(BasicInfo.generalInfo+"<br>");

Execute the above code, you will get the following results:

undefined 
is 3zfp owned object 
info is changed

Note the following points:
1. Use the class name +"."+static variable name to declare a static variable
2. Static variables do not belong to the unique attributes of an instance object of the class, but are shared by the object.
3. Can be used as an instance Object name + "." + static variable name to access.

Global variables:
Global variables are variables with effective access control during the operation of the entire system. They are usually defined at the beginning of a js code, such as:

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. For example:

var copyright = "3zfp owned"; 
var foo =function(fooInfo) 
{ 
 _foo = fooInfo; 
document.write(copyright+"<br>"); 
} 
new foo("foo test"); 
document.write(_foo+"<br>"); 
执行代码,将得到如下结果: 
3zfp owned 
foo test 
但是,这个又有一个注意的地方,function是编译期对象,也就是说_foo这个全局变量要在foo对象被实例化后才能被初始化,也就是说如果将 
new foo(); 
document.write(_foo+"<br>"); 
对调成 
document.write(_foo+"<br>"); 
new foo(); 
系统将提示 "_foo 未定义"。

2. If a local variable attribute with the same name as the global variable is defined, as follows:

var copyright = "3zfp owned";  
var foo =function(fooInfo)  
{  
 var copyright = fooInfo; //同名变量  
 this.showInfo = function()  
 {  
 document.write(copyright+"<br>");  
 }  
}  
new foo("foo test").showInfo();  
document.write(copyright+"<br>");

When you execute the code, you will get the following results:
3zfp owned
foo test
The reason is that the function definition of variables is completed during compilation, that is, the definition of copyright inside foo is completed during compilation, and its scope is only valid within the foo object, and is not related to the external definition. The global variable copyright has nothing to do with it.

The above is the detailed content of Discuss some techniques in using JavaScript variable types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn