JavaScript Basics
# JavaScript Basics
# Data type (8 types) Type
# Number
# String
# Boolean
# Null
# Undefined
Null is a reserved word, but undefined is not. If you access a non-existent variable, the error "var is not defined" will be reported; Access to declared but unassigned variables will result in undefined. The result of typeof null is an object, but null is also a basic type rather than an object. This confusion is because typeof judges the data type based on the low memory bits. The lower three bits of object and null are both 0.
# Symbol ES6
let s = Symbol ( "" )
let s1 = Symbol ( "" ) s == s1 //false s === s //true
let s2 = Symbol . for ( "" )
let s3 = Symbol . for ( "" ) s2 == s3 //true
let s2 = Symbol ( "love" )
let obj = { [ s2 ] : "and peace" } obj //{ [Symbol(love)]: 'and peace' } Object . getOwnPropertySymbols ( obj ) //[ Symbol(love) ] Reflect . ownKeys ( obj ) //[ Symbol(love) ]
# *BigInt ES6
let Num = BigInt ( Math . pow ( two , sixty-three ) )
let Num2 = 100n
# Object
let obj = { key : 'value' , obj : { a : 'a'
} }
# Array
# Date
# RegExp
# Map&Set ES6
let students = new Map ( [ [ 'Lucy' , ninety ] , [ 'Peter' , eighty ] , [ 'Bill' , eighty-five ] ] ) students . get ( 'Lucy' ) //90 students . has ( 'Peter' ) //true students . delete ( 'Peter' ) //true students . get ( 'Peter' ) //undefined students . set ( 'Bill' , ninety ) //Map { 'Lucy' => 90, 'Bill' => 90 } students . get ( 'Bill' ) //90
let foods = new Set ( [ 'Chicken' , 'Noodles' , 'Rice' ] ) foods . add ( 'fish' ) //Set { 'Chicken', 'Noodles', 'Rice', 'fish' } foods . delete ( 'Noodles' ) //true foods . has ( 'Rice' ) //true
# Get basic type: typeof
| |
---|---|
| |
| |
| |
| |
| |
| |
| |
| |
# Get instance type: instanceof
let a = [ ]
typeof a //'object' a instanceof Array //true
class y { }
let t = new y ( ) t instanceof y //true
# Dynamic type&type conversion
# Use JavaScript function conversion
# Automatic conversion using JavaScript
five + null //5 because null=0
five + undefined //NaN is NaN because undefined is converted to a number
"0" + null //'0null' because null='null'
"5" + one //"51" because 1="1"
one + "5" //"51" because 1="1"
"5" - one //4 Because "5"=5
[ one , two ] + one //"1,21" array is converted to string first, and then "1" is added
if ( "str" ) {
//The code here will be executed because "str" can be converted to true
}
# Basic type&reference type
# Basic type packaging
var str = 'str' str . toUpperCase ( )
/* It's equivalent to doing these things var _str = new String(str) str = _str.toUpperCase() */
# Variable declaration
# var
# Variable promotion Hoisting
# let&const ES6
# Block level scope
var h = ten ;
function print ( ) { console . log ( h )
var h ; //Overwrite parent variable
}
print ( ) //undefined
var h = ten ;
function print ( ) { console . log ( h ) //Temporary dead zone
let h ; //Bind to the current scope, and cannot be read before declaration
}
print ( ) //ReferenceError: Cannot access 'h' before initialization
( function ( ) { var hours = twelve } ( ) )
//Equivalent to
{ let hours = twelve }
# function
# class ES6
# import ES6
# global variable
# Judge whether the variable exists
# Naming convention
# Function
# Function declaration
# Parameter transfer
# Object Obejct
# attribute
# Operator
# Comparison operator
# Assignment Operators
# 'and' operators
# 'or' operator
# Extension operator ES6
# For objects
let obj = { a : one , b : two }
let newObj = { ... obj , c : three } //{ a: 1, b: 2, c: 3 }
# For arrays
# assignment
let arr = [ zero , one , two ]
let newArr = [ ... arr , three , four ] //[ 0, 1, 2, 3, 4 ]
let arr_copy = [ ... arr ]
# Destructuring assignment
let [ ... arr_copy ] = arr let [ ar1 , ... ar2 ] = [ zero , one , two , three ] ar1 //0 ar2 //[ 1, 2, 3 ]
let [ ... ar3 , ar4 ] = [ zero , one , two , three ]
//SyntaxError: Rest element must be last element
# The tragedy caused by a semicolon
let val = func ( ) //;
[ a , b ] = [ b , a ] or [ a [ c ] , b [ c ] ] = [ b [ c ] , a [ c ] ]
# For function parameters
let arr = [ zero , one , two ]
function add ( a , b ) {
return a + b }
add ( ... arr ) //1
# Basic operation
# Bitwise Operators
# ternary operator
( ( expression1 , expression2 , expression3 ) => {
if ( expression1 ) {
return expression2 ;
} else {
return expression3 ;
}
} ) ( ) ;
# square brackets
# dot operator
# Loop
# while
# do...while
# forEach
# for…in
# for…of ES6
# Cycle control
loop1 : while ( true ) { loop2 : while ( true ) {
break loop1 ;
}
}
# Control statement
# If statement
# Switch statement
# Some keywords
# new
# delete
# throw
# async&yield
# Error Handling
# Try... catch syntax
# try...catch
try {
//insert code here a . b = c }
catch ( err ) { console . error ( "Error catched: " + err )
}
# throw
try {
throw 'Number is too big'
}
catch ( err ) { console . error ( err )
}
# finally
try {
}
catch ( err ) {
}
finally {
}
# Error object and type
| |
---|---|
| |
| |
| |
| |
| |
| |
# Reference list
-
this https://www.cnblogs.com/xiaohuochai/p/5735901.html -
JavaScript https://www.liaoxuefeng.com/wiki/1022910821149312 -
ECMA Script 6 http://es6.ruanyifeng.com/ -
Execution Context https://juejin.im/entry/58edde2761ff4b00581b93ff -
Symbol https://www.cnblogs.com/diligenceday/p/5462733.html -
MDN -
runoob