Object structure

  • Extract data from an object
let obj = {
    name: 'adc',
    age: '17'} var name = obj.name; var age = obj.age; / / deconstructionlet {name,age} = obj;
Copy the code

Let {a} = {x: 1,y = ‘b’} let {a} = {x: 1,y = ‘b’

The x in front is the match pattern and the x after is the variable

let {x:x,y:y} = {y: 'x',x: 'y'}
let{x:a,y:y} = {y: 10, x: 20} // a is variable A = 20Copy the code
  • Deconstruction can also be set to default values
let {x = 3} = {} // x=3
let {x = 3} = {x: 10} //x=10
let {x:y=5} = {} // y=5
let {x:y=5} = {x:50} //y=50
Copy the code
  • String deconstruction
let [a,b,c] = 'hello'
//a='h,b='e,c='l'
Copy the code
  • Function argument deconstruction
// There are default valuesfunctionadd([x=1,y=2]){ rerurn x+y; } the add ([2, 3]);Copy the code

* Parameter object

function add({x=0,y=0}){
    return x + y;
}
add({x:3,y:4})
Copy the code

expand

  • string
Substring (start,stop)// Intercepts (start, end) substr(strat,length) // Intercepts (start, length) indexOf() // Finds the position of the value, and returns -1 if not foundlet str = 'hello world'; / / STR. The substring (1, 4)'ell'/ / STR. Substr (1, 4)'ello'
Copy the code
  • Methods in ES6
// includes() // Checks whether the string exists. The return value is Booleanif(str.includes('w'// The first parameter is the value to search for, and the second value is the search position str.startwith ('w'Str.endswith (str.endswith (str.endswith ())'w'// Determine if the string is at the end of the original string, with the second argument indicating the location of the search // Repeat () returns a new character, repeating the original character n times'x'.repeat(3) //'xxx'PadStart (); //padEnd()'s'PadStart (4,'xy') / /'xyxs'
's'.padEnd(4,'xy') / /'sxyx'
Copy the code