Object.assign(target, ...source)

1.Object.assign方法只会拷贝源对象自身(不包括原型)的并且可枚举的属性到目标对象,使用源对象的get和目标对象的set,所以会调用相关getter和setter。

通俗点说:源对象的属性值需要配置可枚举,enumerable为true,目标对象的属性值需要可写,writable为true才可以进行拷贝。如果目标对象不可写入,则会TypeError

String类型和 Symbol 类型的属性都会被拷贝。mdn地址:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

为了将属性定义(包括其可枚举性)复制到原型,应使用Object.getOwnPropertyDescriptor()Object.defineProperty() 。使用这两个方法,可以拷贝属性描述器

// 说明源对象的属性需要是可枚举的
const target = {
a: 1,
b: 2
}
const source = {
a: 2
}
Object.defineProperty(source, 'b', { // 不可枚举
value: 3
})
Object.assign(target, source)
console.log(target)// {a: 2, b: 3}
// 说明目标对象的属性,需要是可写入的
const target = {
a: 1
}
const source = {
a: 2,
b: 3
}
Object.defineProperty(target, 'b', { // 不可写
value: 2
})
Object.assign(target,source) // TypeError: Cannot assign to read only property 'b' of object '#<Object>
console.log(target)
// 说明原型上的属性,不会进行拷贝
const target = {
a: 1
}
function Test () {
this.a = 2,
this.b = 3
}
Test.prototype.c = 2
const source = new Test()
Object.assign(target,source)
console.log(source)
console.log(target)

Object.create(proto, propertiesObject)

var obj = Object.create({
_a: 3
}, {
a: {
value: 4,
enumerable: true,
writable: true,
configurable: true
}
})
console.log(obj)

第一个参数为原型对象 obj.__proto__ = proto

第二个参数为对象自身的属性:属性描述器

Object.create 和 Object.assign的更多相关文章

  1. (转)es6中object.create()和object.assign()

    今天学习javascript面向对象,在学习Obejct方法时了解到create方法,偶像想起之前使用的assign方法,顺带查找一番,感觉这篇博客讲解详细,遂转载. 先简单提一下装饰器函数,许多面向 ...

  2. Object.create 以及 Object.setPrototypeOf

    第一部分 Object.crate() 方法是es5中的关于原型的方法, 这个方法会使用指定的原型对象以及属性去创建一个新的对象. 语法 Object.create(proto, [ properti ...

  3. Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create

    Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...

  4. Object.create() 实现

    if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototyp ...

  5. Object.create()和new object()和{}的区别

    Object.create()介绍 Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString ...

  6. js-new、object.create、bind的模拟实现【转载备忘】

    //创建Person构造函数,参数为name,age function Person(name,age){ this.name = name; this.age = age; } function _ ...

  7. js中的new操作符与Object.create()的作用与区别

    js中的new操作符与Object.create()的作用与区别 https://blog.csdn.net/mht1829/article/details/76785231 2017年08月06日 ...

  8. ES5 Object.create 方法

    Object.create(proto[, propertiesObject])The Object.create() method creates a new object with the spe ...

  9. [Javascript] Prototype 2 Object.create()

    function Fencepost (x, y, postNum){ this.x = x; this.y = y; this.postNum = postNum; this.connections ...

随机推荐

  1. Java并发之锁升级:无锁->偏向锁->轻量级锁->重量级锁

    Java并发之锁升级:无锁->偏向锁->轻量级锁->重量级锁 对象头markword 在lock_bits为01的大前提下,只有当是否偏向锁位值为1的时候,才表明当前对象处于偏向锁定 ...

  2. 【图像处理】使用SDL预览webp图片

    写在前面的话 WebP是Google开发的一种图像格式,支持图像数据的有损和无损压缩.保留动画和alpha透明通道数据. 可以创建和JPEG.PNG和GIF图像格式在质量相同或质量更高,但是数据更小的 ...

  3. JMeter中使用交替控制器设置循环次数后都执行一次?

    JMeter在线程组设置循环3次,执行后只执行了一次就停止执行了 排查原因:线程组下添加了一些请求信息(HTTP Cache Manager.HTTP Cookie Manager.HTTP Requ ...

  4. Spring Cloud Eureka 实践(二)

    接上一篇的内容,Eureka服务已经启动成功后,可以尝试开发服务的提供者与消费者,并注册到Eureka来实现服务的发现与调用. 首先,在父工程中继续创建服务提供者的Module,最新的目录结构如下图所 ...

  5. 【HMS Core 6.0全球上线】Toolkit,您的智能辅助编程好帮手

    HMS Core 6.0已于7月15日全球上线.本次版本中,华为HMS Toolkit向广大开发者推出了智能辅助编程助手SmartCoder,帮助开发者轻松高效地集成HMS Core,开发新功能,创建 ...

  6. 将两个byte型拼接成16位二进制,再转化为十进制

    short s = 0; //一个16位整形变量,初值为 0000 0000 0000 0000 byte b1 = 1; //一个byte的变量,作为转换后的高8位,假设初值为 0000 0001 ...

  7. JS020. Array map()函数查到需要的元素时跳出遍历循环,不再执行到数组边界

    Array.prototype.map() map( )  方法创建一个 新数组 *,其结果是该数组中的每个元素是调用一次提供的 函数后的返回值 *.[ MDN / RUNOOB ] * map 添加 ...

  8. Hibernate持久层ORM框架

    一.概念 hibernate交互数据库时,对象的属性转成sql,mybatis直接写sql,性能更高: 二.

  9. ysoserial payloads/JRMPClient

    ysoserial payloads/JRMPClient 环境:JDK8u102 payloads/JRMPClient可以配合exploit/JRMPListener模块来使用 1.在自己服务器上 ...

  10. call、apply、bind三者比较

    var obj={a:1}; var foo={ getA:function(item1,item2){ return this.a+item1+item2 } } // apply绑定参数为数组,一 ...