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)

原文地址:https://www.cnblogs.com/mary-123/p/12232166.html

时间: 2024-08-06 20:25:09

Object.assign()用法讲解的相关文章

Object.assign的用法

工作中使用的Object.assign 类的赋值 var initData = { a:'', b:'' } var oldData = { a:'ww', b:'ee' } var newData = Object.assign({},initData,oldData) console.log(newData) 原来用vue的时候,代码重置是一个个清空的,现在不用了 this.Form = Object.assign({}, this.subRuleForm, this.$options.da

ES6的Object.assign()基本用法

Object.assign方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target). 例如: const target = {a:1}, const source1 = {b:2} const source2 = {c:3}; Object.assign(target, source1,source2); target   // {a:1,b:2,c:3} Object.assign  方法的第一个参数是目标对象,后面的参数都是源对象. 注意:如果目标对象与源

ngx-bootstrap使用03 Alerts组件、利用Object.assign复制对象

1 Alerts 该组件用于给用户操作提供反馈信息或者提供一些警告信息 2 用法 2.1 下载ngx-bootstrap依赖 参考博文:点击前往 2.2 在模块级别导入AlertModule模块 技巧01:由于AlertModule是一个工具组件,在实际开发中一般都是在共享模块进行导入的 import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; impo

Object.assign()方法

转载:http://www.cnblogs.com/zhaowenxin/p/6160676.html 对象的扩展 1.ES6中,对象的属性和方法可简写:对象的属性值可不写,前提是属性名已经声明: 1 var name = "zhangsan"; 2 var password = "1111111"; 3 var obj = { 4 name, 5 password, 6 arr:[1,2,3,4], 7 sayName(){ 8 console.log(this.

ES6对象方法Object.assign()

1  基本用法 Object.assign方法用于对象的合并,将源对象( source )的所有可枚举属性,复制到目标对象( target ). [javascript] view plain copy var target = { a: 1 }; var source1 = { b: 2 }; var source2 = { c: 3 }; Object.assign(target, source1, source2); target // {a:1, b:2, c:3} Object.ass

es6 javascript对象方法Object.assign()

es6 javascript对象方法Object.assign() 2016年12月01日 16:42:34 阅读数:38583 1  基本用法 Object.assign方法用于对象的合并,将源对象( source )的所有可枚举属性,复制到目标对象( target ). [javascript] view plain copy var target = { a: 1 }; var source1 = { b: 2 }; var source2 = { c: 3 }; Object.assig

js中的Object.assign接受两个函数为参数的时候会发生什么?

缘由 今天看到一段代码 return Object.assign(func1, func2); 心生疑惑,为什么 Object.assign 的参数可以是函数? 于是有了下面这一堆东西,其实都是老生常谈的东西,可能是岁数大了吧,有些片段都快丢失了,哈哈 prototype js 中 万物皆是对象!!! proto(隐式原型)与 prototype(显式原型) 对象具有属性proto,可称为隐式原型 实例(对象)的 proto === 构造(该实例)函数的 prototype 函数 Functio

浅谈ES6的Object.assign()浅拷贝

注意: 1.Object.assign() 只是一级属性复制,比浅拷贝多深拷贝了一层而已.用的时候,还是要注意这个问题的. 2.简单实现深拷贝的方法,当然,有一定限制,如下:JSON.parse(JSON.stringify());思路就是将一个对象转成json字符串,然后又将字符串转回对象. Object.assign()方法 特点:浅拷贝.对象属性的合并 代码如下: var nObj = Object.assign({},obj,obj1); 解析:花括号叫目标对象,后面的obj.obj1是

ES6中Object.assign() 方法

1. 对象合并Object.assign 方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象上.如下代码演示: var target = {a: 0}; var source1 = {b: 1}; var source2 = {c: 2}; Object.assign(target, source1, source2); console.log(target); // 输出 {a: 0, b: 1, c: 2} 1-1 如果目标对象与源对象有同名属性,或多个源对象有同名