In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Ecmascript use variables need to be declared, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
The use of variables by ecmascript does not require declaration. In ecmascript, it is not necessary to declare a variable before using it; the reason: when the ECMAScript interpreter encounters an undeclared identifier, it creates a global variable with that variable name and initializes it to the specified value.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
ECMAScript variables do not need to be explicitly type declared
Variable names need to follow some simple rules.
Declare variable
Variables in ECMAScript are defined with the var operator (short for variable) plus the variable name. For example:
Var test = "hi"
In this example, the variable test is declared and its value is initialized to "hi" (string). Because ECMAScript is weakly typed, the interpreter automatically creates a string value for test without explicit type declaration.
You can also define two or more variables with a var statement:
Var test1 = "hi", test2 = "hello"
The previous code defines the variable test1 with an initial value of "hi" and a variable test2 with an initial value of "hello".
However, variables defined with the same var statement do not have to have the same type, as follows:
Var test = "hi", age = 25
In addition to (again) defining test, this example defines age and initializes it to 25. Even if test and age belong to two different data types, this definition is perfectly legal in ECMAScript.
Unlike Java, variables in ECMAScript do not have to be initialized (they are initialized behind the scenes, as discussed later). Therefore, the following line of code is also valid:
Var test
In addition, unlike Java, variables can hold different types of values. This is the advantage of weakly typed variables. For example, you can initialize a variable to a value of type string, and then set it to a numeric value, as follows:
Var test = "hi"; alert (test); test = 55 domestic alert (test)
This code will output string and numeric values without a problem. However, as mentioned earlier, when using variables, a good coding habit is to always store values of the same type.
Named variable
Variable names need to follow two simple rules:
The first character must be a letter, underscore (_), or dollar sign ($)
The remaining characters can be underscores, dollar signs, or any alphanumeric character
The following variables are legal:
Var test;var $test;var $1 * * var _ $te$t2
Well-known variable naming rules
Just because the syntax of variable names is correct, doesn't mean they should be used. Variables should also follow one of the following famous naming conventions:
Camel labeling method
The first letter is lowercase, and the following letters begin with uppercase characters. For example:
Var myTestValue = 0, mySecondValue = "hi";
Pascal labeling method
The first letter is capitalized, and the following letters begin with uppercase characters. For example:
Var MyTestValue = 0, MySecondValue = "hi"
Hungarian type marking method
Append a lowercase letter (or sequence of lowercase letters) to a variable named after the Pascal notation to indicate the type of the variable. For example, I represents an integer and s represents a string, as follows. "
Var iMyTestValue = 0, sMySecondValue = "hi";
These prefixes are used in this tutorial to make the sample code easier to read:
Type prefix sample array aaValues Boolean bbFound floating point (numeric) ffValue function fnfnMethod integer (numeric) iiValue object ooType regular expression rerePattern string ssValue variant (can be of any type) vvValue variable declaration is not required
Another interesting aspect of ECMAScript (and the main difference from most programming languages) is that you don't have to declare variables before you use them. For example:
Var sTest = "hello"; sTest2 = sTest + "world"; alert (sTest2)
In the above code, first, sTest is declared as the value "hello" of type string. On the next line, sTest is concatenated with the string "world" with the variable sTest2. The variable sTest2 is not defined with the var operator, it is just inserted here as if it had been declared.
When the interpreter of ECMAScript encounters an undeclared identifier, it creates a global variable with that variable name and initializes it to the specified value.
This is the convenience of the language, but it can also be dangerous if variables are not closely tracked. The best habit is to always declare all variables, as in other programming languages.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.