Basic concepts of JavaScript (1) --- basic syntax and basic concepts of javascript
Preface:
Since the beginning of the new year, the main goal of this year is javascript. Recently, reading javascript advanced programming (the third edition) is also a very boring task, so let's repeat what we have read. On the one hand, we will get a better impression. On the other hand, if we have an incorrect understanding, we will also ask the experts in the garden for comments.
The first two chapters of this book are mainly about javascript and the use of javascript in HTML, which is a simple understanding of javascript. From Chapter 3, I started my learning, my reading notes are also written in Chapter 3, and Chapter 3 is the basic concept, mainly explained in four aspects: syntax, data type, flow control statement, and function, today, I will mainly write some basic information about the data types.
Syntax:
The javascript syntax mainly draws on some of C and other C-language syntaxes. Therefore, if you have a C or JAVA language foundation, it may be easier to understand the javascript syntax.
I. Case Sensitive
Javascript is the same as java (I am engaged in java, so java is used as an example). variables, function names, operators, and other content are strictly case sensitive, the hello variable name and Hello variable name are two completely different variables and cannot be confused. In addition, when naming a function, you cannot use keywords as function names. Otherwise, problems may occur, for example, once I had to write a delete method, I didn't remember that delete was a keyword, so I used delete as the function name, but how to get the results at runtime, at first, I thought the code inside the function was wrong, but I deleted all the code in the function and left only one lele.info (). Then I found that it was still effective, later I remembered that delete is a keyword and cannot be used as a function name. It is correct only after the name is changed.
Ii. identifier
The identifier is the property name, variable name, function name, function parameter, and other names. The javascript identifier is the same as the java identifier, and the first must be a letter or underscore (_) or the dollar sign ($). Other letters can be numbers, letters, underscores, and dollar signs. Although there is no hard-coded identifier, it must be written as a camper, but for ease of reading, we still agree to write javascript identifiers as camps.
3. Notes
Javascript comments are the same as java comments. Single-line comments are written as //. Multi-line comments are written as/* comments, in addition to the first and last lines, the comments in multiple lines can have the * or no * sign in the middle;
Iv. Strict Mode
I still don't know much about the content of the strict mode. After all, this part of js is not removed too much. I checked Baidu's content about the strict mode on the Internet, strict mode, as its name implies, enables javascript to run in a more rigorous mode. This is a new mode added in ECMAscript5. So what are the reasons for setting the strict mode? There are several main aspects:
- Eliminate some inrigor and irrationality in javascript and reduce some weird behavior;
- Eliminate some insecure aspects of code running to improve the security of code running;
- Improve compiler efficiency and speed;
- It paves the way for future new versions of Javascript.
The "strict mode" reflects the more reasonable, safer, and more rigorous development direction of Javascript. Mainstream browsers, including IE 10, have supported the strict mode. In the strict mode, the running result of the Code may be different from that of the normal mode. Even the code running in the normal mode cannot be run in the strict mode. How can we enter the strict mode?
To enter the strict mode, you must use "use strict". To use the strict mode in the entire script file, you must add "use strict" in the first line of the script file ", at this time, the entire script file will run in strict mode. If it is not placed in the first line, it is invalid, especially when multiple script files are integrated into one file, pay more attention to it. In the following example, the content in the first <script> label is the strict mode, and the second is the normal mode:
<Script> "use strict" v = 1; console.info (v); // running result: "Uncaught ReferenceError: v is not defined "</script> <script> s = 2; console.info (this. s); // running result: "2" </script>
As you can see, in strict mode, values cannot be assigned to undeclared variables, but in normal mode, undeclared variables are assigned by default as global variables. If you place "use strict" in the first line of a single function, the entire function runs in strict mode, as shown below:
Function testUseStrict () {"use strict"; d = 3; console.info (d); // running result: "Uncaught ReferenceError: d is not defined"} testUseStrict ();
Put "use strict" in the first line of the function, then the entire function will run in strict mode. This method provides us with an idea to integrate script files, we can put the entire script file into an anonymous function and immediately execute this anonymous function, as shown below:
<Script> (function () {"use strict"; var v = 11; console.info (v); // running result: 11}) (); </script>
As for the strict mode, you can access the following link. This is much clearer, and I will mark the place where the strict mode is used in javascript learning later. Link to: http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
V. Statements
Javascript statements are almost the same as java statements. They all end with semicolons, but the difference is that a semicolon must be added to the end of a line in java. However, in javascript, a line ends with no extra points, the parser can determine the end of the statement as follows:
var a1 = 2 + 2 var a2 = 5 - 1; console.info(a1);//4 console.info(a2);//4
In addition, in java, if statements can be written as such if (condition )......., javascript can also be written in this way, but it may cause some problems in some cases, whether it is the no-plus sign at the end or the if condition statement does not add;
Keywords:
Some keywords and reserved words exist in javascript language. These keywords can be used to indicate the start or end of a control statement, or the term executes some specific operations. The keywords are reserved by language, therefore, it cannot be used as an identifier. You can search for keywords and reserved words online. I will not write these reserved words here.
I am writing this article for the time being. I will continue to update javascript learning. I hope to learn it with you. If you have any questions, thank you.