第八节 数组与箭头函数

数组填充

//实例的fill方法,fill接收三个参数,填充内容,其实位置,结束位置(注意结束位置)
  let arr = ['com' , 'http' , 'wwww' , '163']; let fillResult = arr.fill('replace' , 1 , 3); console.log(fillResult);

 

数组循环

//数组循环(重点)

let arr2 = ['com' , 'http' , 'wwww' , '163'];

let arrentries = arr2.entries();

for(let item of arr2){
    console.log(item);
}

for(let [index , val] of arr2.entries()){
  console.log("索引是:",index + "----值是:" + val)
}


console.log(arrentries.next().value);//输出[0, "com"]
console.log(arrentries.next().value);//输出[1, "http"]
console.log(arrentries.next().value);//输出[2, "wwww"]
console.log(arrentries.next().value);//输出[3, "163"]

数组查找includes方法:

//includes方法
/*该方法的第二个参数表示搜索的起始位置,默认为0。
如果第二个参数为负数,则表示倒数的位置,
如果这时它大于数组长度(比如第二个参数为-4,但数组长度为3),
则会重置为从0开始。*/


console.log(arr2.includes('com' , 0));
//箭头函数


let add = (a ,b)=>a+b;

console.log(add(3 ,4));


function reduce(a , b=1){
  if(a == 0){
    throw new Error("报错了!");//主动抛异常
  }
  return a-b
}

console.log(reduce(0));

 

posted @ 2018-04-20 10:53  JeneryYang  阅读(699)  评论(0编辑  收藏  举报