Object.create 和 Object.assign
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的更多相关文章
- (转)es6中object.create()和object.assign()
今天学习javascript面向对象,在学习Obejct方法时了解到create方法,偶像想起之前使用的assign方法,顺带查找一番,感觉这篇博客讲解详细,遂转载. 先简单提一下装饰器函数,许多面向 ...
- Object.create 以及 Object.setPrototypeOf
第一部分 Object.crate() 方法是es5中的关于原型的方法, 这个方法会使用指定的原型对象以及属性去创建一个新的对象. 语法 Object.create(proto, [ properti ...
- Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create
Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...
- Object.create() 实现
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototyp ...
- Object.create()和new object()和{}的区别
Object.create()介绍 Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString ...
- js-new、object.create、bind的模拟实现【转载备忘】
//创建Person构造函数,参数为name,age function Person(name,age){ this.name = name; this.age = age; } function _ ...
- js中的new操作符与Object.create()的作用与区别
js中的new操作符与Object.create()的作用与区别 https://blog.csdn.net/mht1829/article/details/76785231 2017年08月06日 ...
- ES5 Object.create 方法
Object.create(proto[, propertiesObject])The Object.create() method creates a new object with the spe ...
- [Javascript] Prototype 2 Object.create()
function Fencepost (x, y, postNum){ this.x = x; this.y = y; this.postNum = postNum; this.connections ...
随机推荐
- Cookie在哪里看
更多java学习请进: https://zhangjzm.gitee.io/self_study
- NOIP模拟「random·string·queen」
T1:random 我又来白剽博客了: 详细证明请看土哥 土哥写的不是很详细,我在这里详细写一下: 首先,对于f[n]的式子: 加一是那一个对的贡献,大C是选其余的几个数,\(2^ ...
- JD 评论晒图爬虫
JD 评论晒图爬虫 #coding=utf-8 import requests import re import os __author__ = 'depy' """ j ...
- Apache Dolphin Scheduler - Dockerfile 详解
Apache DolphinScheduler 是一个分布式去中心化,易扩展的可视化 DAG 工作流任务调度系统.简称 DS,包括 Web 及若干服务,它依赖 PostgreSQL 和 Zookeep ...
- Django学习day06随堂笔记
每日测验 """ 今日考题 1.什么是FBV与CBV,能不能试着解释一下CBV的运作原理 2.模版语法的传值需要注意什么,常见过滤器及标签有哪些 3.自定义过滤器,标签, ...
- Elaticsearch倒排索引
ES倒排索引基本原理 索引(index)可以分为正序索引(Forward Indexes)和倒排索引(Inverted Index)两种.在关系型数据库中使用索引可以避免数据检索走全表扫描,将检索的时 ...
- 小程序跳转H5及其他页面
一.小程序和公众号 答案是:可以相互关联. 在微信公众号里可以添加小程序. 图片有点小,我把文字打出来吧: 可关联已有的小程序或快速创建小程序.已关联的小程序可被使用在自定义菜单和模版消息等场景中. ...
- phpstorm一直 updating indices刷新
解决方法: File-> 选中 Invalidate Caches/Restart ->选中 Invalidate Caches/Restart
- C# lambda 实现 Ascii 排序
var dir = new Dictionary<string, string>(); dir.Add("channelId", "1& ...
- Java面向对象系列(8)- Super详解
场景一 场景二 场景三 场景四 注意:调用父类的构造器,super()必须在子类构造器的第一行 场景五 场景六 super注意点 super调用父类得构造方法(即构造器),必须在构造方法得第一个 su ...