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. Linux 安装 Harbor 私有镜像仓库

    下载 最新发行:https://github.com/goharbor/harbor/releases # 下载文件 wget https://github.com/goharbor/harbor/r ...

  2. Django图片上传和前端展示

    1 - 模型 class Book(models.Model): #定义图书模型 book_name = models.CharField(max_length=100,verbose_name='书 ...

  3. Python - 基本数据处理函数round()、int()、floor()、ceil()

    前言 对每位程序员来说,在编程过程中数据处理是不可避免的,很多时候都需要根据需求把获取到的数据进行处理,取整则是最基本的数据处理.取整的方式则包括向下取整.四舍五入.向上取整等等.下面就来看看在Pyt ...

  4. Python - 进度条库 tqdm

    前言 在写生成器的时候,网上看到一个进度条库,感觉蛮有意思,记录下 这个库感觉只有在调试的时候会用到,不做深入学习 内置库,不需要安装 示例代码 from tqdm import tqdm for i ...

  5. MongoDB(6)- BSON 数据类型

    BSON BSON是一种二进制序列化格式,用于在 MongoDB 中存储文档和进行远程过程调用 跟 JSON 的数据结构很像,但是支持更丰富的数据类型 数据类型 数据类型 序号 别名 备注 Doubl ...

  6. 富文本编辑器-SpringBoot

    目录 简介 Editor.md 基础工程搭建 数据库设计 基础项目搭建 文章编辑整合(重点) 图片上传问题 表情包问题 文章展示 简介 项目地址:https://gitee.com/zwtgit/ri ...

  7. Vue设置全局js/css样式

    ''' 配置全局js mian.js: import settings from '@/assets/js/settings' Vue.prototype.$settings = settings; ...

  8. Flex语法和常用鼠标手势

    Flex弹性和模型 1.display : flex/inline-flex ;(设置给氟元素) flex : 将对象作为弹性伸缩盒显示: inline-flex : 将对象作为内联块级弹性伸缩显示: ...

  9. 深入xLua实现原理之Lua如何调用C#

    xLua是腾讯的一个开源项目,为Unity. .Net. Mono等C#环境增加Lua脚本编程的能力.本文主要是探讨xLua下Lua调用C#的实现原理. Lua与C#数据通信机制 无论是Lua调用C# ...

  10. python函数模块

    python函数模块 学习完本篇,你将会深入掌握 函数相关知识 1.函数定义 2.函数如何调用 3.递归函数以及匿名函数使用 模块相关知识 1.模块如何导入调用 2.函数调用顺序 函数定义 函数代码块 ...