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!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
