类型
1.Number2.String
3.Boolean
4.Undefined
5.Null
6.Symbol
7.Object
variables
1. var2. let
3. const
Conditionals
1. if 2. else 3. esle if 4. ternary operator 5. switchLogical Operators
1. && 2. || 3. !Functions
Data Structror
1.Array2.Object
scope
ES6
EcmaScript 6let and const
advanced functions
const first = () =>{
const greet = 'Hi';
const second = () =>{
alert(greet)
}
return second
}
const newFunc= first();
/*
*/
advancend arrays
forEach,map,filter,reduceforEach不改变原数组,对数组内每个元素操作
map遍历数组,需要输出元素,它创建了一个新的数组
filter 过滤器,遍历条件
reduce
const array = [1,2,10,16];
const double =[];
array.forEach(num => double.push(num*2));
console.log(double)
console.log(array)
const mapArray = array.map(num => num*2);
console.log('map',mapArray)
const filterArray = array.filter(num=> num>5);
console.log('filter',filterArray)
const reduceArray = array.reduce((accumulator, sum) => {
return accumulator +sum;
},11);
console.log('reduce', reduceArray)
/*
foreach (4) [1, 2, 10, 16]
array (4) [2, 4, 20, 32]
map (4) [2, 4, 20, 32]
filter (2) [10, 16]
reduce 40
*/
advanced objects
reference type,context,instantiationclass Player{
constructor(name,type){
this.name = name;
this.type = type;
}
introduce(){
console.log(`Hi I am ${this.name}, I'm a ${this.type}`);
}
}
class Wizard extends Player{
constructor(name,type){
super(name,type);
}
play(){
console.log(`WWWWWWWWeeeeeee I'm a ${this.type}`);
}
}
const wizard1 = new Wizard('Shelly', "Healer");
/*
wizard1.introduce()
Script snippet #27:7 Hi I am Shelly, I'm a Healer
*/
Es7 Es8
es7 .includes() ```js const array=['bird','cat','dog']array.includes('bird') //true array.includes('panda') //false
```js
const square = (x) => x**2
square(5) //25
es8
.padStart() .padEnd()
'Turtle'.padStart(10) //' Turtle'
'Turtle'.padEnd(10) //'Turtle '
Object.values Object.entries Object.keys
let obj ={
username0: 'Santa',
username1: 'Rudolf',
username2: 'Mr.Crinch'
}
Object.keys(obj).forEach((key,index) => console.log(key,obj[key]))
/*
username0 Santa
username1 Rudolf
username2 Mr.Crinch
*/
Object.values(obj).forEach(value => console.log(value))
/*
Santa
Rudolf
Mr.Crinch
*/
Object.entries(obj).forEach(value => console.log(value))
/*
(2) ['username0', 'Santa']
(2) ['username1', 'Rudolf']
(2) ['username2', 'Mr.Crinch']
*/
Es10
.flat() 展平数组 默认是1级,可更改参数,less than 50
//.flat
const array1 = [1,2,3,4,5];
array1.flat();
//(5) [1, 2, 3, 4, 5]
const array2 = [[1],2,[3,4],5];
array2.flat();
//(5) [1, 2, 3, 4, 5]
const array3 = [[1],2,[3,4],[[5]]];
array3.flat();
//(5) [1, 2, 3, 4, Array(1)] //[5]
const array4 = [[1],2,[3,4],[[5]]];
array4.flat(2);
//(5) [1, 2, 3, 4, 5]
//清理数组
const entries = ['bob', 'sally',,,,,,'cindy']
entries.flat();
//(3) ['bob', 'sally', 'cindy']
.flatMap()
.trimStart()
.trimEnd()
const userProfiles = [['commanderTom',23],['derekZlander',40],['hansel',18]]
const userObj=Object.fromEntries(userProfiles)
console.log(userObj)
/*
commanderTom: 23
derekZlander: 40
hansel: 18
*/
Object.entries(userObj)
/*
(2) ['commanderTom', 23]
(2) ['derekZlander', 40]
(2) ['hansel', 18]
*/
advanced loop
const basket =['apples','oranges','grapes'];
//1.
for(let i =0;i<basket.length;i++){
console.log(basket[i]);
}
/*
apples
oranges
grapes
*/
//2.
basket.forEach(item => {
console.log(item)
})
/*
apples
oranges
grapes
*/
//3. for of
//Iterating - arrays,strings
for(item of basket){
console.log(item)
}
/*
apples
oranges
grapes
*/
const detailedBasket = {
apples: 5,
oranges: 10,
grapes: 1000
}
//4. for in - properties
//enumerating - objects
for(item in detailedBasket){
console.log(item)
}
ES2020
BigIntNullish Codescing Operator ??
Optional Chaining Operator ?.
let will_pokemon = {
pikachu: {
species: 'Mouse pokemon',
height: 0.4,
weight: 6
}
}
let andri_pokemon = {
raichu:{
species: 'Mouse pokemon',
height: 0.8,
weight: 30
}
}
let weight = will_pokemon.pikachu.weight;
console.log('weight',weight)
//weight 6
let weight2 =''
if(andri_pokemon.pikachu&&andri_pokemon.pikachu.weight)
weight2 = andri_pokemon.pikachu.weight;
else
weight2= undefined;
console.log('weight2',weight2)
//weight2 undefined
//es2020
let weight3 = andri_pokemon?.pikachu?.weight;
console.log('weight3',weight3)
//weight3 undefined
globalThis
globalThis
Debugging
const flattened = [[0,1],[2,3],[4,5]].reduce((a,b) => a.concat(b),[]);
//console.log(flattened)
//(6) [0, 1, 2, 3, 4, 5]
//console.log()
const flattened1 = [[0,1],[2,3],[4,5]].reduce((a,b) =>{
console.log('b',b);
console.log('a',a);
return a.concat(b)
} ,[]);
//debugger
const flattened2 = [[0,1],[2,3],[4,5]].reduce((a,b) =>{
debugger
return a.concat(b)
} ,[]);
Modules
IIFE