KinoOfficial / ES6-course

0 stars 0 forks source link

箭头函数(arrow function),扩展运算符 #2

Open KinoOfficial opened 1 year ago

KinoOfficial commented 1 year ago

默认参数 function fn(x=5){ //默认参数为5 console.log(x) }

KinoOfficial commented 1 year ago

箭头函数 //这三个为一个函数 //标准的箭头函数写法 let fn = (m) => {return m} //如果只有一个函数,默认参数的括号可以省略 let fn1 = m => {return m } //如果只想返回一个值(没有别的操作),可以省略括号和return let fn2 = (m) =>m

特别的,箭头函数的this指向定义时所在的对象

KinoOfficial commented 1 year ago

数组的扩展 ...扩展运算符(Spread Operator) 可以把数组和类数组结构拆分成以逗号分隔的参数序列 function fn(a,b,c,d){ return a+b+c+d } console.log(fn([1,2,3,4])); //输出错误,因为只传了一个参数 console.log(fn(...[1,2,3,4])); //10

特别的,合并数组: let arr1=[1,2] let arr2=[3,4] let arr3=[...arr1, ...arr2] console.log(arr3) //[1,2,3,4]

合并对象: let obj1={ a:1, b:2 } let obj2={ c:3, d:4 } let obj3={ ...obj1,...obj2 } console.log(obj3) //{a:1,b:2,c:3,d:4} //

KinoOfficial commented 1 year ago

对象的扩展 如果对象的属性值是变量,变量的名字跟属性名同名,键值对可以省略 let name='li' let age = 1 let obj={name,age} console.log(obj) //{name:'li',age:1}

KinoOfficial commented 1 year ago

对象的解构赋值

let obj={ a:1, b:2, c:3 } let {a,b,c} = obj console.log(a,b,c) //1 2 3