JavaScript syntax, sentences, reserved keywords, variables

Chapter 3 Basic Concepts

3.1 Syntax

3.1.1 Case sensitivity

  1. The variable name test and Test are completely different

3.1.2 Identifier: the name of a variable, function, attribute, or function parameter

  1. Naming rules
    • The first character must be a letter, underscore, or dollar sign $
    • Other characters can be letters, underscores, dollar signs, numbers
  2. Use camel case format: the first letter is lowercase, and the first letter of each remaining word is uppercase.
    • for example:myName、herAge。
    • The camel case naming is not mandatory, but it can be regarded as a best practice.

3.1.3 Notes

Including single-line comments and block-level comments.

  1. Single-line comments: start with two slashes. As follows:
// alert(“HelloWorld!”) 
  1. Block-level comments: start with a slash and an asterisk (/*), and end with an asterisk and a slash. As follows:
/*
这是一个
多行的
块级注释
*/

3.1.4 Strict mode

  1. Definition: A different analysis and execution model defined for JavaScript.
  2. Instructions:
    • To enable strict mode throughout the script, you can add code at the top “use strict”;.
    • You can also include this pragma above the inside of the function.
  3. Use effect: In strict mode, some uncertain behaviors in ECMAScript3 will be handled, and errors will also be thrown for some unsafe operations. In strict mode, the execution effect of js will be very different.

3.1.5 Statement

  • Statements in ECMAScript end with a semicolon, but it is not required.
  • If the semicolon is omitted, the parser determines the end of the statement.
  • It is recommended not to omit the semicolon, because writing the parser will eliminate the need to spend time guessing where to insert the semicolon.

3.2 Keywords and reserved words

ECMA-262 describes a set of keywords with specific purposes and a set of reserved words that cannot be used as identifiers.

  1. Keywords: can be used to indicate the beginning or end of a control statement, or to perform specific operations, etc.
  2. Reserved words: Although reserved words have no specific purpose in this language, they may be used as keywords in the future.

3.3 Variables

  • ECMAScript variables are loosely typed, that is, they can be used to store any type of data.
  • When defining a variable, use the var operator followed by the variable name. For example var message, of course, you can also initialize the variable directly when defining it, for examplevar message = ‘hi’ ;
  • This paragraph means that a string "hi" is stored in the variable message. Initializing a variable like this does not mark it as a string type. The process of initialization just assigns a value to the variable.
  • Therefore, to focus on, you can modify the value type while modifying the variable. E.g:
var message = ‘hi’ ;
message = 100 ;   //有效,但不推荐
//这个例子代表变量message一开始保存了一个字符串“hi”,然后该值又被一个数字值100取代了。
  • One thing to note is that the variable defined with the var operator will become a local variable in the scope of the variable. In other words, if you use var to define a variable in a function, the variable will be destroyed after the function exits. E.g:
function test(){
	var  message = ‘hi’ ; //局部变量
} ;
test();
alert(message); //错误

//为什么是错误?
//这里,变量message是在函数里用var定义的,当函数被调用时,就会创建该变量并为其赋值。而在此之后,这个变量会立即被销毁。所以在执行alerat()那行代码的时候message已经被销毁了,因此报错。

So, how to solve it?

  • By omitting the var operator inside the function, you can create a global variable. Example:
function test(){
	message = ‘hi’ ; //局部变量
} ;
test();
alert(message); // hi
//在函数内部不用var会创建全局变量。
//但我们并不提倡这种做法,因为局部作用域中定义的全局变量很难去维护。
//所以我们应该选择在开始就定义好所有的变量。

Guess you like

Origin blog.csdn.net/bentou_/article/details/109405767