Home > Article > Web Front-end > Commonly used knowledge points about JavaScript
Grammar
Statement expression
A sentence is a group of words that completely expresses a certain meaning, consisting of one or more It consists of phrases, and is connected between them by punctuation marks or connecting words.
Statements are equivalent to sentences, expressions are equivalent to phrases, and operators are equivalent to punctuation marks and connectives.
Expressions in JavaScript can return a result value.
var a = 3 * 6;
var b = a; = a;
Declaration statement, because it declares the statement#The result value of the statement
The side effects of the expression
var a = 42;var b = a++;
42 (Assign the value to
b),
Then add # to the value of
a
##1
; ++ When is in front, such as ++a, its side effects Incrementing (a) occurs before the expression returns the result and the side effect of a++ occurs after. ++a++ Will generate
ReferenceErrorError var a = 42; var b = (a++); The operator concatenates multiple independent expression statements into one statement: var a = 42,b; b = (a++,a) a ; // 43
b; // 43The delete operator is used to delete attributes in objects and elements in arrays. var obj = {
var obj = { var obj = { obj.a; // true obj.a;if(str) {
//
Extract all vowel lettersmatches = str.match(/[aeiou]/g);
if(matches) return matches;
## vowels("Hello World" ); // ["e","o","o"]Use the side effects of assignment statements to combine two if statements into one
function vomels (str){
var matches;
// Extract all element letters
if(str && (matches = str.match(/[aeiou]/g))){
Return matches;
Context rules
1.Braces
・ credtrap ##Assume functionbar()
has been defined
var a = {foo: bar() # { {}; / / "[object Object]"
{} + []; // 0
Object destructuringfunction getData() { b: "foo"
}; } var { a , b } = getData(); can also be used as object destructuring of function named parameters to facilitate implicit assignment of object properties.
Function foo({a,b,c}){
//
No longer needs this:// var a = obj.a, b = obj.b, c = obj.c console.log(a,b,c)
}
foo({
o" [1 ,2,3]
Operator precedencevar a = 42;
var b = "foo" ;
a && b; // "foo"
a || b; // 42
Short circuit
For &&
and ||, if the result can be obtained from the left operand, the right operand can be ignored
Operands, we call this phenomenon a short circuit.a && b || c ? c || b ? a : c && b : a
Because the && operator has a higher priority than ||, and || ## The priority of # is higher than? :.
(a && b || c) ? (c || b) ? a : (c && b) : aAssociation
Operator association is either left to right or right to left, depending on whether the combination starts from the left or from the right.
var a = foo() && bar();through through [Execute first foo() Follow the execution order from left to right
var a , b , c; a = b = c = 42; = ( c = 42)) var a = 42;var b = "foo";
var c = false; var d = a && b || c ? c || b ? a : c && b : a; d; // 42 ((a && b) || c)?
((c || b) ? a : (c && b)) : a
Now let’s execute
one by one 1. (a && b)The result is "foo".
2."foo" || c The result is"foo ”.
3,The first one? In "foo"
is true. 4.(c || b) The result is "foo".
4.(c || , "foo"
is the true value.6.a The value of is 42
The value of is 42
The value of is
42Error
Errors that occur during the compilation phase are called early errors. Syntax errors are a type of early errors. Use try..catch
to capture. Syntax errors are generally reported by browsers SyntaxError
Use variables in advance
Temporary dead zone
TDZ
let
Scope block## a = 2; // ReferenceError
let a;
}Function parameters
function foo(a = 42, b = a + 1){ console.log(a,b)} foo(); // 42 43 foo(undefined); // 42 43 foo(5); // 5 6 foo( void 0, 7); // 42 7foo(null) // null 1 try.. finally The code in finally will always be try will be executed after, if there is catch , it will be executed after
###catch ######. ######You can think of the code in finally as a callback function, which is always executed last.
Switch
You can think of it as a simplified version of if..else if.. else .
Switch (a) {
case 2:
# #Execute some code
}
Mixed environment
JavaScript
Dynamically createscript, tags and add them to the
DOM of the page.var greeting = "Hello World";
var el = document.createElement("script"); el.text = "function foo() { alert(greeting); setTimeout(foo,1000)}";
Document.body.appendChild(el); If el.src If the value is set to URL
, you can use