Basic concepts
Javascript is an interpreted language, and the browser acts as an interpreter. When executing js, it is explained first and then executed within the same scope. When interpreting, the variables defined by the two keywords function and var will be compiled. After the compilation is completed, the variables will be executed from top to bottom and assigned values.
case sensitive
Everything in ECMASCript (including variables, function names, and operators) is case sensitive.
1. Variables
Variables are set in memory when they are first used, so they are later referenced in scripts. Declare it before using variables. Variable declarations can be used using the var keyword.
var count, amount, level; // Multiple declarations declared with a single var keyword.
Variable naming
Variable names include global variables, local variables, class variables, function parameters, etc. They all fall into this category.
Variable naming is composed of type prefix + meaningful words, and the readability of variables and functions is increased by camel nomenclature. For example: sUserName, nCount.
Prefix specification:
Each local variable needs to have a type prefix, which can be divided into:
s: represents a string. For example: sName, sHtml;
n: represents a number. For example: nPage, nTotal;
b: represents logic. For example: bChecked, bHasLogin;
a: represents an array. For example: aList, aGroup;
r: represents a regular expression. For example: rDomain, rEmail;
f: represents a function. For example: fGetHtml, fInit;
o: represents other objects not mentioned above, such as: oButton, oDate;
g: represents global variables, such as: gUserName, gLoginTime;
JScript is a case-sensitive language. Creating a legitimate variable name should follow the following rules:
Note that the first character cannot be a number.
It can be followed by any letter or number and underscore, but it cannot be a space variable name, it must not be a reserved word.
JavaScript is a weakly typed language, and JavaScript ignores unnecessary spaces. You can add spaces to the script to improve its readability.
var is a reserved word for javascript, indicating that the following is the variable description, the variable name is a user-defined identifier, and the variables are separated by commas.
If a variable is declared but no value is assigned to it, the variable exists and its value is the Jscript value undefined.
Cases type conversion
In Jscript, operations can be performed on different types of values without worrying about exceptions from the JScript interpreter. Instead, the JScript interpreter automatically changes (casts) one of the data types to another data type and then performs the operation. For example:
Calculation results
Adding the value to the string casts the value to a string.
Adding the boolean value to the string casts the boolean value to a string.
Adding the numeric value to the Boolean value casts the Boolean value to a numeric value.
To explicitly convert a string to an integer, use the parseInt method. To explicitly convert a string to a number, use the parseFloat method.
Lifetime of JavaScript variables: When you declare a variable within a function, you can only access the variable in that function. When exiting the function, this variable will be revoked. This variable is called a local variable. You can use local variables with the same name in different functions, because only functions that declared variables can recognize each variable.
If you declare a variable outside of a function, all functions on the page can access the variable. The lifetime of these variables starts after they are declared and ends when the page is closed.
js variable mind map
2.js data type
jscript has three types -> main data types, two -> composite data types and two -> special data types.
Main (basic) data types
String
Value
Boole
Composite (reference) data type
Object
Array
Special data types
Null
`Undefined`
String data type: String data type is used to represent text in JScript. In js, while both double quotes ("") and single quotes (") can represent strings, and they have little difference. Using double quotes ("") to represent strings is considered the best.
A string value is a string of zeros or more Unicode characters (letters, numbers, and punctuation marks) arranged together.
What is Unicode?
Unicode provides unique values for each character, regardless of platform, program or language. Unicode is developed to provide a unified encoding for processing all characters that exist in the world.
Numerical data type
We need to understand that JScript internally represents all values as floating point values, so there is no difference between integers and floating point values in JScript.
Boolean data type
Boolean (logical) can only have two values: true or false.
js arrays and objects
For details, please see my article -> JavaScript Learning Summary - Array and Object Part
Null Data Type: You can clear the content of a variable by assigning a null value to a variable.
The typeof operator in Jscript will report that the null value is Object type, not type null.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title></title> <script type="text/javascript"> alert(typeof null); </script></head><body></body></html>
null is used to represent an object that has not existed yet, and is often used to represent a function that attempts to return an object that does not exist.
Undefined data type:
The undefined value will be returned in the following situation:
The object attribute does not exist.
The variable was declared but the value was never assigned.
Difference between null and undefined
alert(typeof undefined); //output "undefined" alert(typeof null); //output "object" alert(null == undefined); //output "true"
ECMAScript believes that undefined is derived from null, so they are defined as equal.
alert(null === undefined); //output "false" alert(typeof null == typeof undefined); //output "false"
The types of null and undefined are different, so the output is "false". And === means absolute equality, here null === undefined output false
In addition, here is a relatively important data type - reference data type
Reference data type
JavaScript reference data type is an object stored in heap memory. JavaScript does not allow direct access to the location and operation of the heap memory space. It can only operate the reference address of the object in the stack memory. So the reference type data is actually stored in the stack memory as the reference address of the object in the heap memory. This reference address allows you to quickly find objects stored in heap memory.
Let's demonstrate the process of quoting data type assignment
Of course, adding the name attribute to obj2 is actually adding the name attribute to the objects in the heap memory. Obj2 and obj1 save only the reference address of the heap memory object in the stack memory. Although they are also copied, the object pointed to is the same. Therefore, changing obj2 causes the change of obj1.
Basic type values refer to those simple data segments stored in the stack memory, i.e., a location where such values are completely stored in memory.
The reference type value refers to those objects stored in heap memory, that is, the variable is actually just a pointer, which points to another location in memory, where the object is saved.
In short, the heap memory stores reference values, and the stack memory stores fixed type values.
In ECMAScript, variables can have two types of values, namely the original value and the reference value.
The original values are stored in the stack, which means that their values are stored directly at the location where the variable is accessed. The referenced object stored in the heap, that is, the value stored at the variable is a pointer pointing to the memory where the object is stored.
<script type="text/javascript">var box = new Object(); //Create a reference type var box = "lee"; //The basic type value is the string box.age = 23; //It is weird to add attributes to basic type values, because only objects can add attributes. alert(box.age); //It is not a reference type, and cannot be output;</script>
3.JScript operators
Priority: refers to the order of operations of operators. In layman's terms, it is to calculate which part of the operation first.
Combination: The calculation order of the same priority operator, in layman's terms, is it from which direction to calculate, whether it is left to right or right to left.
Data type conversion and basic wrapping type
String() to string type
Number() to a number type
Boolean() to Boolean type
parseInt: Convert a string to an integer. Start parsing from the beginning of the string, stop parsing at the first non-integer position, and return all integers read earlier. If the string does not start with an integer, NaN will be returned. For example: the value returned by parseInt ("150 hi") is: 150, and the value returned by parseInt ("hi") is: NaN.
parseFloat: Converts a string to a floating point number. Start parsing from the beginning of the string, stop parsing at the first non-integer position, and return all integers read earlier. If the string does not start with an integer, NaN will be returned. For example: parseFloat("15.5 hi") returns the value: 15.5, and parseFloat("hi 15.5") returns the value: NaN.
eval: calculates the string as a javascript expression and returns the execution result, and returns undefined if there is no result.
Basic packaging type
Whenever a basic type value is read, the background will create an object of the corresponding basic wrapper type, so that some methods can be called to manipulate this data. Basic packaging types include Boolean, Number, and String
var box = 'trigkit4'; //Literal box.name = 'mike'; //Invalid attribute box.age = function () { //Invalid method return 22;};//New operator writing var box = new String('trigkit4');//New operator box.name = 'mike'; //Valid attribute box.age = function () { //Valid method return 22;};The String type contains three properties and a large number of built-in methods available
Attribute description
length: Returns the character length of the string
Constructor: Returns the function that creates a String object
prototype: Extend string definition by adding properties and methods
4.js process control
For js process control statements, here are only a few difficult to understand. I won't go into details about the others. A mind map is attached below.
1. The for…in statement executes one or more statements corresponding to each of an object, or each element of an array.
for (variable in [object | array])
statements
parameter:
variable: a required option. A variable that can be either attribute of an object or any element of an array.
object, array: optional. The object or array to traverse on it.
statement: Optional. One or more statements to be executed relative to each property of an object or each element of an array. It can be a compound statement.
Although conditional control statements (such as if statements) require the use of code blocks only when multiple statements are executed (the end of the left curly braces {"start, the right curly braces"}"), the best practice is to always use code blocks.
if(args) alert(args);//Easy to errors if(args){ alert(args);//Recommended to use}5.js function
A function is a reusable block of code that is driven by an event or executed when it is called.
Jscript supports two functions: one is a function inside the language, and the other is created by itself.
JavaScript functions allow no parameters (but brackets containing parameters cannot be omitted), and parameters can be passed to functions for use by functions.
For more information about functions, please visit another article of my article: A summary of JavaScript learning (IV) Function function part
Composition of objects
Method - Function: Procedural, dynamic
Attributes - variables: state, static
Finally, a mind map summarized by the seniors:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.