箭头函数特点:

    1.不具备this和arguments

    2.自己没有this就找上一级的this

更改this指向的方法:

   1.call,apply,bind

   2.var that=this

   3.箭头函数=>

function()转箭头函数的方法:

   1.关键字function去掉,()和{}之间用=>连接,

   2.若只有一个参数,可省略小括号(),

   3.有大括号{}则必须写return,没有大括号则直接是返回值

//普通函数转箭头函数
 function a(b){
   return function(c){
     return b+c
   }
 }

//1.去掉关键字function,()和{}之间用=>连接
  let a=(b)=>{
    return (c)=>{
     return b+c
   }
 }

//2.函数只有一个参数,省略小括号()
 let a=b=>{
    return c=>{
     return b+c
   }
 }
//3.有返回值,去掉return必须去掉{}
  
let a=b=>c=>b+c

一、js生成随机颜色的两种常用方法

1.方法一

function color(){
    return '#' + Math.floor(Math.random() * 0xffffff).toString(16);
}

2.方法二

function color(){
       var r=Math.floor(Math.random()*256);
       var g=Math.floor(Math.random()*256);
       var b=Math.floor(Math.random()*256);
       return "rgb("+r+','+g+','+b+")";
    }

**生成(n-m)之间的随机数  Math.random()*(m-n)+m

二、复选框全选

$("input[class='checkAll']").click(function(){
   $("input[class='checkItem']").prop("checked",this.checked);
})

三、操作数组的方法 

 可直接改变原数组的方法-------pop,push,unshift,shift,splice,reverse,soft

 生成新数组的方法--------------indexOf,lastIndexof,concat,slice

四、forEach,for,for in,for of区别

 例 let arr=[1,2,3,4,5]; arr.b="hh"

  1.   forEach为声明式语法(不关心执行原理),不支持return
arr.forEach(function(val){
     console.log(val)    
  })

    2.for为编程式语法(可看到执行过程);

for(let i=0;i<arr.length;i++){
    console.log(arr[i])
}

   3. for in

  1. 可以遍历出arr的所有属性,包括私有属性
  2. key的数据类型为string
for(let key in arr){
    console.log(arr[key])
}

 

es6的js如何在typescript中引用 js es6方法_返回结果

   4.for of(es6方法) 

  1.  支持return 
  2. 值 of 数组
  3. 不能遍历对象(使用object.keys(obj)可将obj的key值生成新数组,在通过obj[val]遍历对象值)   
for(let val of arr){
    console.log(val)
}

es6的js如何在typescript中引用 js es6方法_返回结果_02

  使用for of遍历obj的方法

 let obj={name:'mary',age:18}

console.log(Object.keys(obj)) //将对象的key值组成新的数组
for(let val of Object.keys(obj)){    
    console.log(obj[val])
}

 

es6的js如何在typescript中引用 js es6方法_数组_03

五、filter方法的使用

    是否操作原数组-------------否

    返回结果-------------过滤后的数组

    回调函数的返回结果------true则将这一项放回新数组中

let newArr=arr.filter(function(item){
    return item>2 && item<5
})
console.log(newArr)

es6的js如何在typescript中引用 js es6方法_数组_04

六、map方法的使用

    是否操作原数组-------------否

    返回结果-------------映射后的新数组

    回调函数的返回结果------回调函数返回什么这一项就返回什么

 例:将数组arr映射出成'<li>1</li><li>2</li><li>3</li><li>4</li><li>5</li>'

var newArr1=arr.map(function(item){
    return `<li>${item}</li>`
})
console.log(newArr1.join(""))

es6的js如何在typescript中引用 js es6方法_返回结果_05

七、includes,find,some,every方法的使用与不同   

 1.includes(),返回值为boolean,找到为true,找不到为false

arr.includes(searchElement)
arr.includes(searchElement, fromIndex)

 2.find();

  1. 不会改变原数组,返回找到的那一项;
  2. 回调函数中返回true则为找到了,停止循环;
  3. 找不到返回undefined

   3.some()找true,并停止循环,返回true,找不到返回false

   4.every()找false,并停止循环,返回false,找不到返回true

let arr=[1,2,3,4,5,53];

//1.includes()

console.log(arr.includes(5)//----->true

//2.find() 找到返回当前项,找不到返回undefined

let newArr=arr.find(function(item){
    //找到数组中含有5的项
     return item.toString().indexOf(5)>-1
})
console.log(newArr)//--------->5


let newArr2=arr.find(function(item){
    //找到数组中含有5的项
     return item.toString().indexOf(6)>-1
})
console.log(newArr2)//--------->undefined

//3.some()
   
let newArr=arr.some(function(item){
    //找到数组中含有5的项
     return item.toString().indexOf(5)>-1
})
console.log(newArr)//--------->true

   八、reduce()    

     是否操作原数组-------------否

     返回结果-------------叠加后的结果

     回调函数的返回结果------

arr.reduce(function(prev,next,index,item){

     //prev数组的第一项
     //next数组的第二项
     //index 索引
     //item 原数组
     //initialValue可选。传递给函数的初始值
    
      return 100; //本次的返回值 会作为下一次的prev

    },initialValue)

   常用实例    

//1.数组每一项求和

let sum=[1,2,3,4,5].reduce(function(prev,next){
  return prev+next
})

console.log(sum)---------->15

//2.数组对象求和
let arr=[{price:10,num:1},{price:20,num:2},{price:30,num:3},{price:40,num:4}]

let sum=arr.reduce(function(prev,next){
    return prev+next.price*next.num
},0)
console.log(sum)------------>300

//3.扁平化数组
// 将[[1,2,3],[4,5,6],[7,8,9]]变成一维数组[1,2,3,4,5,6,7,8,9]

  let newArr=[[1,2,3],[4,5,6],[7,8,9]].reduce(function(prev,next){
     return prev.concat(next)
  })

  console.log(newArr)---------->[1,2,3,4,5,6,7,8,9]