Javascript learning notes (1) Basic knowledge and javascript learning notes
Basic Concepts
Javascript is an interpreted language, and the browser acts as an interpreter. The js execution engine does not execute a row or a row, but analyzes and executes a row or a row.
Delayed script
The defer attribute is defined in HTML4.0.1. Its purpose is to indicate that the script does not affect the Page Structure during execution. That is to say, the script will be executed after the whole page is parsed. Therefore, setting the defer attribute in the <script> element is equivalent to telling the browser to download the file immediately, but the execution is delayed. In the XHTML document, set the defer attributedefer=“defer"
Asynchronous script
html5
Defines the async attribute for <script>. The entire attribute is similar to the defer attribute and is used to change the script processing behavior. Similarly, like defer, async only applies to external script files and tells the browser to download files immediately. However, unlike defer, scripts marked as async are not necessarily executed in the specified order.
The purpose of specifying async is to prevent the page from waiting for the script file to be downloaded and executed, so as to asynchronously load other content on the page. Therefore, it is recommended that the asynchronous script do not operate the DOM during the loading.
Case Sensitive
All variables, function names, and operators in ECMASCript are case sensitive.
1. JScript Variables
The variable is set to memory when it is used for the first time, so that it can be referenced in the script later. Declare variables before using them. You can use the var keyword to declare variables.
Var count, amount, level; // multiple declarations declared with a single var keyword.
Variable name
Variable names include global variables, local variables, class variables, and function parameters.
Variable names are composed of prefix types and meaningful words. The camper naming method is used to increase the readability of variables and functions. For example, sUserName and nCount.
Prefix specification:
Each local variable must have a type prefix, which can be divided:
S: string. For example, sName, sHtml; n: indicates a number. For example: nPage, nTotal; B: Indicates logic. For example, bChecked, bHasLogin; a: indicates an array. For example, aList, aGroup; r: indicates a regular expression. For example, rDomain, rEmail; f: indicates a function. For example, fGetHtml, fInit; o: indicates other objects not involved, such as oButton, oDate; g: indicates global variables, such as gUserName and gLoginTime;
JScript is a case-sensitive language. Follow the following rules to create valid variable names:
Note that the first character cannot be a number.
It can be followed by any letter, number, or underline, but cannot be a space.
Variable names must not be reserved words.
javascript
Is a weak language,JavaScript
Extra spaces are ignored. You can add spaces to the script to improve readability.
var
Yesjavascript
The reserved word, indicating that the following is the description of the variable, the variable name is a user-defined identifier, and the variables are separated by commas.
If a variable is declared but not assigned a value, the variable exists and its value is Jscript value undefined.
Forced type conversion
In Jscript, operations can be performed on different types of values without worrying about exceptions in the JScript interpreter. Instead, the JScript interpreter automatically changes (forced conversion) One of the data types to another, and then executes the operation. For example:
The calculation result value is added together with the string to forcibly convert the value to a string. Adding a Boolean value to a string forcibly converts a Boolean value to a string. Adding a value to a Boolean value forcibly converts a Boolean value to a value.
To explicitly convert a string to an integer, useparseInt
Method. To explicitly convert a string to a number, useparseFloat
Method.
JavaScript
Variable Survival: After you declare a variable in the function, you can only access the variable in the function. After exiting the function, the variable is revoked. This type of variable is called a local variable. You can use local variables with the same name in different functions, because only the declared functions can recognize each of these variables.
If you declare a variable outside the 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, composite data types, and special data types.
Main (basic) Data Types
Boolean string value
Composite (reference) Data Type
Object Array
Special Data Types
Null`Undefined`
String data type: the string data type is used to represent the text in JScript. In JavaScript, although double quotation marks ("") and single quotation marks ('') can represent strings, there is almost no difference between them. However, only double quotation marks ("") are used to indicate that the string is considered optimal.
A string value is a string of zero or more Unicode characters (letters, numbers, and punctuation marks ).
What is Unicode?
Unicode provides a unique value for each character, no matter what platform, program, or language. Unicode is developed to provide unified encoding for all characters in the world.
Numeric data type
We need to understand that JScript internally expresses all the values as floating point values, so there is no difference between integer and floating point values in Jscript.
Boolean data type
Boolean (logical) can have only two values: true or false.
Js array and Object
For more information, see my article-> javascript learning Summary--array and object Section
Null data type: You can assign a null value to a variable to clear the content of the variable.
In Jscripttypeof
Operator will reportnull
The value isObject
Type, rather than typenull
.
null
It is used to indicate objects that do not exist. It is often used to indicate that a function attempts to return a nonexistent object.
Undefined
Data Type:
The undefined value is returned as follows:
The object property does not exist. variables are declared but no value is assigned.
Differences between null and undefined
alert(typeof undefined); //output "undefined" alert(typeof null); //output "object" alert(null == undefined); //output "true"
ECMAScript considers undefined to be derived from null, so they are defined as equal.
alert(null === undefined); //output "false" alert(typeof null == typeof undefined); //output "false"
The type of null is different from that of undefined, so the output is "false ". And = indicates absolute equals. Here null = undefined outputs false
In addition, a more important data type-reference data type is introduced here.
Reference data type
The javascript reference data type is an object stored in the heap memory. JavaScript does not allow direct access to the location and operation in the heap memory space. You can only use the reference address of the operation object in the stack memory. Therefore, data of the reference type is saved in the stack memory as the reference address of the object in the heap memory. You can use this reference address to quickly find objects stored in heap memory.
The following describes how to assign a value to the referenced data type.
Naturally, adding the name attribute to obj2 actually adds the name attribute to the object in the heap memory. What obj2 and obj1 save in the stack memory is the reference address of the heap memory object, although it is also copied, it points to the same object. Therefore, changing obj2 causes the change of obj1.
The basic type value refers to the simple data segments stored in the stack memory, that is, the values are completely stored in a location in the memory.
The reference type value refers to the objects stored in the heap memory, that is, the objects stored in the variable are actually a pointer, which points to another location in the memory and stores the objects.
In short, the heap memory stores reference values, and the stack memory stores fixed type values.
InECMAScript
The variable can have two types of values: the original value and the reference value.
Raw values are stored on the stack (stack
), That is, their values are directly stored in the variable access location. Reference Values are stored in the heap (heap
), That is, the value stored in the variable is a pointer (point
), Pointing to the memory of the storage object.
<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 strange 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: The operation sequence of operators. Generally speaking, it is the first part of calculation.
Conciseness: the order in which operators of the same priority are calculated. In general, the value is from which direction, left to right, or right to left.
Data Type Conversion
Convert String () to String type
Number () to numeric type
Convert Boolean () to Boolean type
parseInt
: Converts a string to an integer. Start from the beginning of the string, stop parsing at the first non-integer location, and return all integers read before. If the string does not start with an integer, NaN is returned. For example, parseInt ("150 hi") returns 150, and parseInt ("hi") returns NaN.
parseFloat
: Converts a string to a floating point number. Start from the beginning of the string, stop parsing at the first non-integer location, and return all integers read before. If the string does not start with an integer, NaN is returned. For example:ParseFloat ("15.5 hi") returns 15.5, and parseFloat ("hi 15.5") returns NaN.
Eval: calculates the string as a javascript expression and returns the execution result. If there is no result, undefined is returned.
4. js Process Control
For js process control statements, it is hard to understand. Do not repeat the rest. And so on.
1. for... in statements correspond to each of an object or each element of an array, and execute one or more statements.
for (variable in [object | array])statements
Parameters:
variable
: Required. A variable, which can be any attribute of an object or any element of an array.
object
,array
: Optional. The object or array to traverse.
statement
: Optional. One or more statements to be executed relative to each attribute 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 (starting with "{" and ending with "}") only when multiple statements are executed ), but the best practice is to always use code blocks.
If (args) alert (args); // if (args) {alert (args); // recommended}
Js process control statement Mind Map
5. js Functions
A function is an event-driven or reusable code block that is executed when it is called.
Jscript
Two types of functions are supported: one is internal functions of the language, and the other is self-created.
JavaScript
The function allows no parameters (but the parentheses containing the parameters cannot be omitted). You can also pass parameters to the function for use.