Object.assign()用法講解


// 將所有可枚舉屬性的值從一個或多個源對象復制到目標對象。它將返回目標對象。
      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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM