2017-12-26es6-函数扩展

扩展

1、字符串扩展

//字符串截取
var str = 'helloword' ;
//原来的 substring(1,4)  从哪个 到哪个之间
console.log(str.substring(1,4)); // =>'ell'
substr(1,4) 从第一个往后数第四个
 console.log(str.substring(1,4)); // =>'ello'
//indexOf() 用来匹配当前是否匹配成功
str.indexOf('el')       //1
str.indexOf('eee')   // -1
if(str.index0f("h") !=-1){}  //判断是否存在“h”在字符串str里面
str.startsWith('hello' ,4)  //false 字符 串是否头部   第二个参数表示搜索的位置
str.startsWith('hello' )  //true
str.endsWith('hello')  //字符 串是否尾部   第二个参数表示搜索的位置
str.includes("h") //true     //是否存在字符串   第二个参数表示搜索的位置
str.repeat('3')  //重复3次
//补全
var s = 's';
s.padStart(4,'xy');   //xyxs
s.padEnd(4,'xy');   //sxyx
var lo = 's123456'
lo.padStart(4, 'xy')   //s123456  超出长度不补全

2、模板字符串 ****

//前
$(".list").html('
  • '+item.name+'
  • '); //后 $(".list").html(`
  • ${item.name}
  • `); ` ${ 变量 } `

    3、数值扩展

    Number.parseInt();
    Number.parseFloat();
    

    4、函数扩展
    用于分页居多

    //ES5
    function fun(x, y){
      x = y || 'aa';
    console.log(x,y)
    }
    //ES6
    function fun(x, y="aa"){
    console.log(x,y)
    }
    //箭头函数
    fun =(x,y)=>{
    console.log(x,y)
    }
    
    rest 参数

    ...变量名 ES5下类似于arguments

    //ES5
    function fun(...values){
    console.log(values[2])  //333 
    }
    fun(1,22,333);
    //ES6
    
    

    ...扩展运算符:表示里面含有多个参数
    针对数组

    let fun = function(x,y,z) {
            console.log(x);
            console.log(z);
    } 
     var arr = [1,2,3,4];
    fun(...arr);
    

    数组合并

     //ES5
    var arr1 = [1,2];
    arr1.concat([3,4,5]);
    //ES6
     let arr1 = [11,22];
    let arr2 = [1,2,3];
    [...arr2 , ...arr1]
    

    你可能感兴趣的:(2017-12-26es6-函数扩展)