js es6对象方法扩展

1. Object.is 判断两个值是否完全相等
    console.log(Object.is(120,120));//true
    console.log(Object.is(120.121));//false
    console.log(Object.is(NaN,NaN));//true
    console.log(NaN === NaN);//false

 

2. Object.assign 对象的合并
    // 2. Object.assign 对象的合并
    const config1 = {
        host: 'localhost',
        port: 3306,
        name: 'root',
        pass: 'root',
        test1: 'test1'
    }
    const config2 = {
        host: 'http://www.baidu.com',
        port: 33060,
        name: 'baidu.com',
        pass: "query",
        test2: 'test2'
    }
    //相同属性后面的会覆盖前面的,不同属性不影响且最终都会在新对象中有该属性 常用来做配置合并
    console.log(Object.assign(config1, config2));//{host: 'http://www.baidu.com', port: 33060, name: 'baidu.com', pass: 'query', test1: 'test1', test2: 'test2'}

 

3. Object.setPrototypeOf 设置原型对象 Object.getPrototypeOf获取原型对象
    // 3. Object.setPrototypeOf 设置原型对象 Object.getPrototypeOf获取原型对象
    const company = {
        name: '字节'
    }
    const cities = {
        place: ['北京','上海','深圳']
    }
    Object.setPrototypeOf(company,cities);
    console.log(Object.getPrototypeOf(company));//{name: '字节'}
    console.log(company);//name: "字节" [[Prototype]]: Object place: (3) ['北京', '上海', '深圳']

 

posted @ 2021-09-26 00:15  jerryfish  阅读(131)  评论(0编辑  收藏  举报