I. composition of JavaScrip
ECMAScript + DOM + BOM(Document Object Model)
JavaScrip is introduced
JavaScript import method: tags are inlined and inwritten and imported externally
Put it in the header, no effect page downloaded from, not loaded into or not living in the DOM, can’t call the event you need to add the window statement to execute after the page is rendered: Window.onload =function(){// do something} To prevent errors, use the window.onload event to load the JavaScript code; If you don’t use the window.onload event, When external is introduced, nothing is added to script elements (no content is allowed)
JavaScrip is used
JS annotation usage
Single-line comment // space use condition/range debug multi-line comment CTRL +shirt+/ Use condition/range function description /**/
JS popover & debug
Alert Popover Prompt dialog popover Confirm popover console.log (‘ console output ‘) document.write (‘ printed in document ‘) document.writeln (‘ printed in document ‘)
Debug methods: 1: comment 2: console console.log(‘ ‘) 3. Debug
identifier
Constant: a variable that never changes: a variable whose value may change Definition constant: const operator Definition variable ——var operator let operator JS variables are weak variables that can hold any type of data identifier, referring to the name of a variable, function, attribute, or function argument
Identifier naming conventions
1. The first character must be a letter, underscore (_), or a dollar sign ($). Other characters can be letters, underscores, dollar signs, or digits
2. Case sensitive
3. No Spaces
4. The name cannot be a keyword or reserved word
Naming methods for identifiers
Hungarian nomenclature Attribute + Type + Object Description Small Hump Nomenclature The first letter of the first word is in lower case, the first letter of subsequent words is capitalized, and all other letters are in lower case. It is recommended to use the large hump nomenclature with all uppercase identifiers. Follow the small hump nomenclature 2. Variables and attributes: start with a noun 3. Methods and functions: start with a verb 4. Constants: all letters in uppercase, if multiple word combinations occur, separated by underscores (PI_API) 5. Constructor: uppercase, follow the big hump naming method to avoid naming conflicts: 1. Let a = {}; Function (){}
Common JS data types
Undefine Value undefined Null Null (empty Object) Number Number NaN (not a numeber) String String “” Boolean Boolean value true false 0: false false Other numbers: true Object Object Symbol Unique and immutable data types There are roughly two types of data types
Value type Variable value types: numeric, Boolean, NULL, undefined, string. The exchange of variables is equal to the creation of a new space in a new scope, which is unrelated and irrelevant to the previous space.
Reference types Variable reference types: object, array, function. The exchange of variables does not create a new space. Instead, the object or method points to an existing space (that is, an address) with the previous object or method. It is just like a man who had the key to his house before, and then he got married and gave a key to his wife. At this time, both of them have the key to their home, but the home is still a home
JavaScript operators and priorities
The assignment operator = + = – = * = / arithmetic operators + – * / % % remainder related to dividend relational operator > < < = > = = = (see value equals) = = = (see value types Congruent)! = return booleans ASCII code 97 A, 65 A logical operator | | (or) && (with)! (not)! True/false! False //true increment operator ++ a++ // a=a+1 decrement operator — b– // a=b-1 condition operator? Statement 1: (Statement 2)
var a=2012; Var b= "leap year "; Var c= 0; ((a%4==0&&a%100! = 0) | | (a % 400 = = 0))? (b) : (c);Copy the code
Attached example: The relationship between the values of the various data types and NaN on underpay and NULL, etc
The priority of the operator
Logic non-arithmetic relational logic versus logical or conditional operators assignment operators (parentheses can boost precedence)
Conversion of data types
Implicit conversion
Digits => Character string
- + (hyphen) 123 + “” “123” number + string = String
String – Digit = – * / %
- “123” 123-0
- “123” / 1
- “123” * 1
- + “123”
- “123” % The number that is larger than the number string
Explicit conversion
String => numbers 1.Number () var a =”123″; ParseInt () => string a.tostring (); parseFloat () => string a.tostring (); parseFloat () => string A.tostring (); String(a)
A. toFixed () a.toFixed (2) A. toFixed (2) A. toFixed (2) ToFixed ((0.1+0.3).tofixed (2).tofixed (2).tofixed (2).tofixed (2).tofixed (2).tofixed (2).tofixed (2).tofixed (2).tofixed (2).tofixed (2)