search
HomeWeb Front-endJS TutorialDiscuss some techniques in using JavaScript variable types

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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment