ES6的部分笔记2

Set()
	let arr=[1,2,3,4,1,2,3,4]
    
    //去重
    let result = [..new Set(arr)]
    console.log(result)
	
	//交集
	let arr2 = [2,3,4,4]
    let result = [...new Set(arr)].filter(item=>{
	/*
	//第一种
	let s2 = new Set(arr2)
    if(s2.has(item)){
		return true
       }else{
		return flase
    	}
    	*//*第二种*/
        new Set(arr2).has(item)
    })
    
    //并集
    let union = [...new Set([...arr,...arr2])]
    console.log(union)

	//差集
	let diff = [...new Set(arr)].filter(item=>
    !(new Set(arr2).has(item))        )
    console.log(diff)
Map
	//声明
	let m = new Map()
    //添加元素
	m.set('name','aaa')
	m.set('change',function(){
		console.log("11")
    })
	let key ={
        school:'aaaaa'
	}
    m.set(key,['a','b','c'])
	//size
    console.log(m.size)
    console.log(m)
	//删除
	m.delete('name')
	//获取
    console.log(m.get('change'))
	console.log(m.get(key))
Class类
	//ES5
	function Phone(brand,price){
		this.brand = brand;
        this.price = price
    }
    Phone.prototype.call = function(){
	console.log("call....")
	}
    let Huawei = new phone("huawei",5999)
	Huawei.call()
	console.log(Huawei)

	//ES6
	class Shouji{
		constructor(brand,price){               this.brand = brand;
			this.price = price;
        }
        call(){
			console.log("call....")
        }
        let onePlus = new Showji('1+',1999)
	console.log(onePlus)
    }
	function Phone(){

    }
	Phone.name = 'shouji'
	Phone.change = function(){
		console.log('zzzz')
    }
	Phone.prototype.size = '5.5inch'
	let nokia = new Phone()
	console.log(nokia.name)//undefined,不属于实例对象
	nokia.change()//实例对象无法打印
	console.log(nokia.size) //5.5inch
	class Shouji(){
		static name = 'shouji'
        static change(){
			console.log('change')
        }
    }
	let nokia = new Shouji()
    //static属于类而不属于实例对象
    console.log(nokia.name)//undefined
    console.log(Shouji.name)//change    
继承
	//ES5
	function Phone(brand,price){
		this.brand = brand;
		this.price = price
	}
	Phone.prototype.call = function(){
	console.log("call....")
    }
	function SmartPhone(brand,price,color,size){
	Phone.call(this,brand,price)
        this.color = color;
        this.size = size
    }
	SmartPhone.prototype = new Phone;
	SmartPhone.prototype.constructor = SmartPhone;
	SmartPhone.prototype.photo = function(){
	console.log("paizhao")
    }
	SmartPhone.prototype.playGame = function(){
	console.log("wanyouxi")
    }
    const chuizi = new SmartPhone('chuizi',2499,'black','6inch')
    console.log(chuizi)
	//ES6
	class Phone{
		constructor(price,brand){
            this.price = price;
            this.brand = brand
        }    
        call(){								console.log("call....")
        }
    }    
    class SmartPhone extends Phone(){
	constructor(brand,price,color,size){
		super(brand,price);
        this.color = color;
        this.size = size
    }
     photo(){
		console.log("photo")
       }
     playGame(){
		console.log("wanyouxi")
       }   
      call(){
          //不能在普通成员里面用super调用父类同名方法
		console.log("call....")
      }  
   }
     const xiaomi = new Smartphone('xiaomi',799,'black','4inch')
	console.log('wanyouxi')
  
call的get和set
	class Phone{
		get price(){
			console.log("price...")
            return 'iloveyou'
            //动态变化
        }
        set price(newVal){
			console.log("price修改了")
            //判断
        }
    }
	//实例化对象
	let s = new Phone()
    console.log(s.price)

	s.price = 'free'//触发set的方法
Number
	//Number.EPSILON js的最小精度
	function equal(a,b){
		if(Math.abs(a-b)<Number.EPSILON){
			return true
		}else{
			return false
        }
	}
	console.log(0.1+0.2===0.3)//false	
	console.log(equal(0.1+0.2,0.3))//true
	
	//Number.isFinite是否为有限数
	console.log(Number.isFinite(100))//true
	console.log(Number.isFinite(100/0))//false
	console.log(Number.isFinite(Infinity)//false
    console.log(Number.isNaN(123))//false
    //Number.parseInt,字符串转整数
    console.log(Number.parseInt(11111love))//11111
    console.log(Number.parseFloat(1.1111love))//1.1111
	//Number.isInteger 判断是否为整数
    console.log(Number.isInteger(5))//true
	//Number.trunc 数字的小数部分抹掉
	console.log(Math.trunc(3.5))//3
	//Math.sign()判断是正数负数或是0,返回结果1 0 -1
	console.log(Math.sign(-21))//-1
	
	//Object.is 判断两个值是否完全相等
	console.log(Object.is(123,123))//true
	console.log(Object.is(NaNNaN))//true
	console.log(Object.is(NaN===NaN))//false
	const config1 = {
		host:'localhost',
        port:3306,
        name:'root',
        pass:'root'
    }
    const config2 = {
		host:'http;//1111.com',
        port:33060,
        name:'11111',
        pass:'22222'
    }
	console.log(Object.assign(config1,config2))
	//后面的属性覆盖前面的属性,
	//Object.setPrototypeof 设置原型对象
    const school = {
		name:'111'
    }
    const cities = {
		xiaoqu = ['a','b','c']
    }
    Object.setPrototypeof(school,cities)//为school设置原型
	console.log(school)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值