How to quickly get started with VUE3.0: Enter learning
1. The concept of variable
: In program development, it is often necessary to customize some symbols to mark some names and give them specific uses, such as variable names, function names, etc. These symbols are called an identifier.
Definition rules
Legal identifiers are: it, It, age66, _age, $name.
Illegal identifiers are: to, to, 798lu.
Note that
when multiple words are required in the identifier, the common representation method is the underline method (such as user_name ), camel case (such as userName) and Pascal method (such as UserName). Readers can unify and standardize the naming method according to development needs. For example, the underscore method is usually used for naming variables, and the camel case method is usually used for naming function names.
reserved keywords: refers to words that have been defined in advance and given special meanings in the JavaScript language.
Future reserved keywords: refers to words that are reserved and may become reserved keywords in the future.
reserved keywords
Keywords cannot be used as variable names and function names, otherwise syntax errors will occur during JavaScript loading.
Keywords reserved for the future
When defining identifiers, it is recommended not to use future reserved keywords to avoid errors when converting them to keywords in the future.
Concept: Variables can be regarded as containers for storing data.
For example: a cup holding water, the cup refers to the variable, and the water in the cup refers to the data stored in the variable.
Syntax: Variables in JavaScript are usually declared using the var keyword, and the naming rules for variable names are the same as identifiers.
Examples: legal variable names (such as number, _it123), illegal variable names (such as 88shout, &num).
Note that
although variables in JavaScript can be assigned without declaring them in advance, the var keyword can be directly omitted to assign a value to the variable. However, since JavaScript uses dynamic compilation, it is not easy to find errors in the code when the program is running. Therefore, it is recommended that readers develop the good habit of declaring variables before using them.
constants: Constants can be understood as quantities whose values never change during the running of the script.
Features: Once defined, it cannot be modified or redefined.
For example: Pi in mathematics is a constant, and its value is fixed and cannot be changed.
Syntax: The const keyword is new in ES6, which is used to define constants.
Constant naming rules: follow the identifier naming rules. It is customary to always use capital letters for constant names.
The value of a constant: A constant can be specific data when assigned, or it can be the value of an expression or a variable.
2. Data type
Data in JavaScript: When using or assigning a value, determine the corresponding type according to the specific content of the setting.
But every computer language has its own supported data types, and JavaScript is no exception.
Reference data types will be introduced in detail in later chapters.
The Boolean type is one of the more commonly used data types in JavaScript and is usually used for logical judgments.
ture | false
represents the "true" and "false" of things, strictly following case, so the true and false values only represent Boolean types when they are all lowercase.
Numeric types in JavaScript do not distinguish between integers and floating point numbers. All numbers are numeric types.
As long as the given value does not exceed the range allowed for numerical specification in JavaScript.
NaN non-numeric
type Character type (String) is a character sequence composed of Unicode characters, numbers, etc. We generally call this character sequence a string.
Function: Represents the data type of text.
Syntax: Character data in the program is enclosed in single quotes (") or double quotes ("").
Question: How to use single quotes inside single quotes, or double quotes inside double quotes?
Answer: Use the escape character "" to escape.
When using special symbols such as newline and Tab in a string, you also need to use the escape character "".
Why is data type detection needed? Use the following example to explain?
Please analyze and tell the data type of variable sum and why?
Think about the answer: The variable sum is a character type.
Process analysis: As long as one of the operands of the operator "+" is a character type, it represents character splicing. As for the two variables involved in the operation in this case, num1 is of numeric type and num2 is of character type, so the final output variable sum is the string concatenated between num1 and num2.
Thoughts and conclusions: When there are requirements for the data types involved in operations during development, data type detection needs to be performed.
JavaScript provides the following two methods for detecting data types:
The typeof operator returns the uncalculated type of the operand in string form.
When using typeof to detect the type of null, object is returned instead of null.
Since everything in JavaScript is an object, you can use the extension function of Object.prototype.toString.call() to distinguish data types more accurately.
The return value of Object.prototype.toString.call(data) is a character result in the form of "[object data type]". (The return value can be observed through console.log().)
Data type conversion - to Boolean
application scenario: often used in expressions and process control statements, such as data comparison and condition judgment.
Implementation syntax: Boolean() function.
Note: The Boolean() function will convert any non-empty string and non-zero value to true, and convert empty strings, 0, NaN, undefined and null to false.
Demonstration example: Determine whether the user has input content.
Analyze Boolean(con):
Data type conversion - conversion to numeric
application scenarios: When receiving data passed by users for calculation during development, in order to ensure that all data involved in the calculation are numeric, it is often necessary to convert it.
Implementation syntax: Number() function, parseInt() function or parseFloat() function.
Demonstration example: Complete automatic summation based on user input.
There are certain differences in the use of functions that convert numeric types.
Note that
in actual development, it is also necessary to judge whether the converted result is NaN. Only when it is not NaN can the operation be performed. At this time, you can use the isNaN() function to determine. When the given value is undefined, NaN, and {} (object), it returns true, otherwise it returns false.
Data type conversion - conversion to character type
implementation syntax: String() function and toString() method.
Differences in implementation methods: The String() function can convert any type into a character type; except for null and undefined, which do not have a toString() method, other data types can complete character conversion.
Demonstration example: Complete automatic summation based on user input.
Note that
when the toString() method performs data type conversion, the value can be converted into a string in the specified base through parameter settings, such as num4.toString(2), which means first converting decimal 26 to binary 11010, and then converting Character data.
concept: An expression can be a collection of various types of data, variables, and operators.
The simplest expression can be a variable.