ES6中的Object、apply、call

      // 将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
      const target = {a: 1, b: 2}
      const source = {b: 4, c: 5}
      const returnedTarget = Object.assign(target, source)
      // output: Object { a: 1, b: 4, c: 5 }
      console.log(target)
      // output: Object { a: 1, b: 4, c: 5 }
      console.log(returnedTarget)

      // 浅拷贝复制对象 Object.assign()拷贝的是属性值。假如源对象的属性值是一个对象的引用,那么它也只指向那个引用
      const obj = {a: 1}
      const copy = Object.assign({}, obj)
      // { a: 1 }
      console.log(copy)

      // 合并对象
      const o1 = {a: 1}
      const o2 = {b: 2}
      const o3 = {c: 3}
      const obj1 = Object.assign(o1, o2, o3)
      console.log(obj1) // { a: 1, b: 2, c: 3 }
      console.log(o1)  // { a: 1, b: 2, c: 3 }, 注意目标对象自身也会改变。

      // 合并具有相同属性的对象
      const o11 = {a: 1, b: 1, c: 1}
      const o22 = {b: 2, c: 2}
      const o33 = {c: 3}

      const obj2 = Object.assign({}, o11, o22, o33)
      console.log(obj2) // { a: 1, b: 2, c: 3 }

      // 创建一个新对象
      const person = {
        isHuman: false,
        printIntroduction: function () {
          console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`)
        }
      }
      const me = Object.create(person)
      me.name = 'Matthew' // "name" is a property set on "me", but not on "person"
      me.isHuman = true // inherited properties can be overwritten
      me.printIntroduction()

      // entries返回一个给定对象自身可枚举属性的键值对数组
      const obj = {
        a: 'somestring',
        b: 42
      }
      for (let [key, value] of Object.entries(obj)) {
        console.log(`${key}: ${value}`)
      }
      console.log(obj)

      // 可以将 Map 转化为 Object
      const map = new Map([['foo', 'bar'], ['baz', 42]])
      const obj = Object.fromEntries(map)
      // { foo: "bar", baz: 42 }
      console.log(obj)

      // 可以将 Array 转化为 Object
      const arr = [['0', 'a'], ['1', 'b'], ['2', 'c']]
      const obj2 = Object.fromEntries(arr)
      // { 0: "a", 1: "b", 2: "c" }
      console.log(obj2)

      const obj = {
        prop: 42
      }
      // 被冻结的对象再也不能被修改 不能向这个对象添加新的属性,不能删除已有属性,
      // 不能修改该对象已有属性的可枚举性、可配置性、可写性,以及不能修改已有属性的值。
      // 此外,冻结一个对象后该对象的原型也不能被修改 freeze() 返回和传入的参数相同的对象
      Object.freeze(obj)
      obj.prop = 33
      console.log(obj.prop)

      // 判断两个值是否是相同的值
      let l = Object.is('foo', 'foo')
      console.log(l)

      // simple array
      var arr = ['a', 'b', 'c']
      console.log(Object.keys(arr)) // console: ['0', '1', '2']

      // array like object
      var obj = {0: 'a', 1: 'b', 2: 'c'}
      console.log(Object.keys(obj)) // console: ['0', '1', '2']

      // array like object with random key ordering
      var anObj = {100: 'a', 2: 'b', 7: 'c'}
      console.log(Object.keys(anObj)) // console: ['2', '7', '100']

      var obj = {foo: 'bar', baz: 42}
      console.log(Object.values(obj)) // ['bar', 42]

      // array like object
      var obj = {0: 'a', 1: 'b', 2: 'c'}
      console.log(Object.values(obj)) // ['a', 'b', 'c']

      // array like object with random key ordering
      // when we use numeric keys, the value returned in a numerical order according to the keys
      var an_obj = {100: 'a', 2: 'b', 7: 'c'}
      console.log(Object.values(an_obj)) // ['b', 'c', 'a']

      // valueOf() 方法返回指定对象的原始值
      // Array:返回数组对象本身
      let array = ['ABC', true, 12, -5]
      // true
      console.log(array.valueOf() === array)

      // Date:当前时间距1970年1月1日午夜的毫秒数
      let date = new Date(2013, 7, 18, 23, 11, 59, 230)
      // 1376838719230
      console.log(date.valueOf())

      // Number:返回数字值
      let num = 15.26540
      // 15.2654
      console.log(num.valueOf())

      // 布尔:返回布尔值true或false
      let bool = true
      // true
      console.log(bool.valueOf() === bool)

      // new一个Boolean对象
      let newBool = new Boolean(true)
      // valueOf()返回的是true,两者的值相等
      // true
      console.log(newBool.valueOf() == newBool)
      // 但是不全等,两者类型不相等,前者是boolean类型,后者是object类型
      // false
      console.log(newBool.valueOf() === newBool)

      /**
       * apply() 方法调用一个具有给定this值的函数,以及作为一个数组(或类似数组对象)提供的参数。
       * call()方法的作用和 apply() 方法类似,区别就是call()方法接受的是参数列表,而apply()方法接受的是一个参数数组
       **/
      var numbers = [5, 6, 2, 3, 7]
      var max = Math.max.apply(null, numbers)
      console.log(max)
      var min = Math.min.apply(null, numbers)
      console.log(min)

      // 用 apply 将数组添加到另一个数组
      var array = ['a', 'b']
      var elements = [0, 1, 2]
      array.push.apply(array, elements)
      console.info(array) // ["a", "b", 0, 1, 2]

      /**
       * 使用 call 方法调用父构造函数
       * */
      function Product(name, price) {
        this.name = name
        this.price = price
      }

      function Food(name, price) {
        Product.call(this, name, price)
        this.category = 'food'
      }

      function Toy(name, price) {
        Product.call(this, name, price)
        this.category = 'toy'
      }

      var cheese = new Food('feta', 5)
      var fun = new Toy('robot', 40)
      console.log(cheese)
      console.log(fun)
原文地址:https://www.cnblogs.com/ronle/p/11758459.html