es6 入坑笔记(二)---函数扩展,箭头函数,扩展运算符...

函数扩展

1.函数可以有默认值 function demo( a = 10,b ){}

2.函数可以使用解构 

function demo( { a = 0,b = 0 } = {} ){

}

3.函数参数最后可以多一个逗号

function demo(a,b,c,){

}

1.与for等父子域不同

function(a){

  let a=10;

}

会报错,因为a已经被定义

2.

function move({x, y} = { x: 0, y: 0 })

{ return [x, y]; }

move({x: 3, y: 8});// [3, 8]

move({x: 3}); // [3, undefined]

move({}); // [undefined, undefined]

move(); // [0, 0]

undefined才会触发函数参数的默认

箭头函数

简略形式:(参数)=>(return 的数据)

完整形式:

(参数)=>{

  语句

  return数据

}

作用:解决了this对象,当前this指向最顶层的申明对象

eg:

let json = {

  name:"zjj",

  demo:function(){

    setTimeout(()=>{

      alert(this.name);//这里的this不再是当前的运行时对象,而是最顶层的json

    }),200

  }

}

1.箭头函数里没有arguments转用...

//错误代码

let show = function(){

  alert(arguments);

}

//正确代码

let show = function(...args){

  alert(args);

}

2.箭头函数不能作为构造函数

扩展运算符

1....

1.散列的数据变成数组

demo(1,2,3,4);

function(a,b,...c){

  //a = 1;b = 2 , c = [3,4];

}

2.数组变成散列的数据

let arr1 = [1,2,3];//将arr1复制给另一个数组

let arr2 = [...arr1];

坑:必须放在最后

2**3=8;

猜你喜欢

转载自blog.csdn.net/qq_38052995/article/details/83833991
今日推荐