JS ES6拓展

正则拓展

  • u修饰符的使用:在正则式中使用unicode匹配
  • y修饰符的使用:y修饰符和g修饰符相似,也是全局匹配,不同在于,g只要剩余位置中存在匹配即可,y修饰符确保匹配从剩余第一个位置开始

进制拓展

  • 二进制表示方法(0b):

    console.log(0b11111)

  • 八进制表示方法(0o):

    console.log(0o)

Number拓展

  • Number自动转十进制:

    console.log(Number("0b111"))

  • Number.isFinite //检查一个数值是否有限

  • Number.isNaN() //检查一个值是否是NAN,es6

  • window.isNaN() //检查一个值是否是NAN,es5

// es5 全局window.isFinite() window.isNaN()

// 传统方法先调用Number()进行数据类型转换后再判断

//Number上这两个新方法自对数值有效

  • console.log(Number.parseInt("12.34")) //12
  • console.log(Number.parseFloat("12.34")) //方法可以把一个字符串解析成浮点数
    //判断一个数值是否为整数
  • console.log(Number.isInteger(25))

//安全整数 -253-253

  • console.log(Number.MAX_SAFE_INTEGER)

  • console.log(Number.MIX_SAFE_INTEGER)

  • console.log(Number.isSafeInteger("a"))

Math对象拓展

  • Math.trunc //返回一个数的整数部分
  • Math.sign //判断一个数是正数、负数、还是零
  • Math.cbrt //判断一个数的立方根

函数参数拓展

  • ES6函数参数默认值的设置
function fun2(x,y="world"){
    console.log("fun2",x,y)
}
  • 与解构赋值结合使用
function fun3({x,y=5}={}){
    console.log("fun3",x,y)
}
  • 参数默认值的位置
function fun4(x=1,y){
    console.log("fun4",x,y)
}
fun4()  ----1 undefined
fun4(2)  ----2 undefined
//给x传undefined可以触发默认值的设定
fun4(undefined,1)

rest参数

  • 将函数多余的参数,放入values数组中
  • rest参数只能是最后一个参数
    function add(...values){

}

箭头函数中的this指向

  • 箭头函数没有自己的this,它的this是继承而来,默认指向在定义它的所处对象
  • 箭头函数中的this,就是外层代码块中的this
  • 箭头函数不能当作构造函数,不可以使用new命令,也不能使用argument对象
function Person(name,age){
    this.name=name;
    this.age=age;
    setInterval(function(){
        console.log(this)   --------this是window
        console.log(this.name,this.age)
    },1000)
}
let p1=new Person("tom",18)

--------------------------
function Person2(name,age){
    this.name=name;
    this.age=age;
    setInterval(()=>{
        console.log(this)   ----------this是person
    },1000)
}
-----------------------
箭头函数不能当作构造函数,不可以使用new命令,也不能使用argument对象
let fun=()=>{
    return 1
}
var f=new fun()

拓展运算符

  • 拓展运算符(...),rest参数的逆运算
  • 将一个数组的数据转为用逗号分割的参数序列
深拷贝
---------JSON

let a1=[1,2]
-------let a2=a1.concat()
console.log(a2)

let a1=[1,2]
---------let a2=[...a1]
console.log(a1===a2)
console.log(a2)
但是如果数组里面是引用数据类型,那么concat和...都是浅拷贝

ES6数组新增方法

  • 伪数组(document获得的dom节点,argument)
  • Array.from()方法用于将伪数组的对象转为真正的数组
let ps1=document.querySelectorAll("p")
let ps2=Array.from(ps1).filter(p=>{
    return p.textContent.length>5
})
console.log(ps2,ps2.length)
  • Array.of()方法用于将一组数值,转换成数组
let a1=new Array()     ------空
let a2=new Array(3)    -----3个长度,为空
let a3=new Array(3,4)    ---[3,4]
let a3=Array.of()  -------空
let a4=Array.of(3)  ------[3]
let a5=Array.of(3,4)  ----[3,4]
  • Array.copyWithin(target,start=0,end=this.length)

  • 在当前数组内部:将指定位置的数据复制到其它位置,返回当前数组

  • target:(必需):从该位置开始替换数据

  • start:(可选):从该位置开始读取数据,默认为0

  • end:(可选):到该位置前停止读取数据,默认等于数组的长度

  • Array.find()和findIndex()

  • 在数组中查找第一个符合条件的成员,参数是一个回调函数,返回true/false

let res=[1,4,-5,10].find((value,index,arr)=>{
    console.log("每次遍历数组成员的值",value)
    console.log("数组成员的索引位置",index)
    console.log("原数组",arr)
    console.log("------------------")
    return value <0
})
console.log(ret)

let ret2=[1,4,-5,10].findIndex((value,index,arr)=>{
      console.log("每次遍历数组成员的值",value)
    console.log("数组成员的索引位置",index)
    console.log("原数组",arr)
    console.log("------------------")
    return value <0 
})
区别:Array.find()返回数值,findIndex返回索引
  • Array.fill()使用一个给定值,填充数组
let a1=['a','b'].fill(7)
let a2=new Array(3).fill(7)
let a3=['a','b'].fill(7,1,2)

//Array.keys、values()、entries()返回一个遍历器对象 Iterator
{
    //for    of
    for(let index of['a','b'].keys()){
        console.log(index)
    }


    for(let [index,elem] of ['a','b'].entries()){
        console.log(index,elem)
    }
}
//Array.includes

对象属性拓展

  • 对象方法的简写
 let obj1={
     fun:function(){

     }
 }
 简写:
 let obj2={
     fun(){

     }
 }
  • 属性名表达式

let obj1={}
obj1.foo=true;
obj1['a'+'bc']=123
console.log(obj1)


let obj2={foo:true,abc:123}
console.log(obj2)

let propKey="foo"
let obj3={
    [propKey]:true,
    ['a'+'bc']:123
}

对象新增方法

//Object.is()比较两个值是否相同,与===的行为基本一致

console.log(1=="1")    true
console.log(1==="1")   false
console.log(+0===-0)   true
console.log(NaN===NaN)  false

console.log(Object.is(+0,-0))
console.log(Object.is(NaN,NaN))

//Object.assign用于对象的合并,将源对象的所有可枚举的属性,复制到目标对象上去,可枚举的意思是说在构建对象的时候有enmuable属性设置
let target={a:1}
let source={b:2}
Object.assign(target,source)  //直接在原对象target上修改,相同属性会覆盖

//只有一个参数,参数不是对象,会先转成对象,然后返回
{
    let ret=Object.assign(100)  /Number {100}
    console.log("assign",ret)
}

//非对象的参数出现在合并对象参数位置,无法转成对象的不会抛错

{
    let obj={a:1}
    Object.assign(obj,null)
    console.log("assign",obj)
}
{
    let v1="abc"   ----字符串转对象{index:a,index:b}   0: "4"   1: "5"
    Object.assign({},v1)
    console.log("assign",obj)
}
//字符串和数组可以包装成对象,能够产生枚举属性
//assign拷贝的属性是有限制的,只拷贝源对象本身的属性(不拷贝继承属性)
//也不拷贝不可枚举属性
posted @ 2021-02-25 11:08  abcdefgab  阅读(80)  评论(0)    收藏  举报