当前位置:网站首页>Js - 内置对象
Js - 内置对象
2022-07-30 11:31:00 【sgling】
Object.assgin()方法:
**
Object.assgin()
==> 方法用于将所有可枚举属性的值从一个或多个源对象分配到目标对象。它将返回目标对象。
JavaScript Demo: Object.assign()
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(source);
// expected output: Object { b: 4, c: 5 }
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
语法:
Object.assign(target, ...sources);
参数:
target: 目标对象 sources: 源对象
返回值:
target: 目标对象
描述:
**
如果目标对象中的属性具有相同的键,则属性将被源对象中的属性覆盖。后面的源对象的属性将类似地覆盖前面的源对象的属性。 Object.assign 方法只会拷贝源对象自身的并且可枚举的属性到目标对象。该方法使用源对象的[[Get]]和目标对象的[[Set]],所以它会调用相关 getter 和 setter。因此,它分配属性,而不仅仅是复制或定义新的属性。如果合并源包含getter,这可能使其不适合将新属性合并到原型中。为了将属性定义(包括其可枚举性)复制到原型,应使用Object.getOwnPropertyDescriptor()和Object.defineProperty() 。 String类型和 Symbol 类型的属性都会被拷贝。 在出现错误的情况下,例如,如果属性不可写,会引发TypeError,如果在引发错误之前添加了任何属性,则可以更改target对象。
备注: 「Object.assign 不会在那些source对象值为 null 或 undefined 的时候抛出错误。」
示例:
复制一个对象
const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }
合并对象:
const o1 = { a: 1 };
const o2 = { b: 2 };
const o3 = { c: 3 };
const obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1); // { a: 1, b: 2, c: 3 }, 注意目标对象自身也会改变。
合并具有相同属性的对象:
const o1 = { a: 1, b: 2, c: 3 };
const o2 = { a: 1, b: 2 };
const o3 = { c: 3 };
const obj = Object.assign({}, o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
边栏推荐
- RY-D1/1 Voltage Relay
- Microsoft SQL server hacked, bandwidth stolen
- Performance testing of API Gateway APISIX on Google Cloud T2A and T2D
- 美团内推+校招笔试题+知识点总结
- Apifox 生成接口文档 教程与操作步骤
- I built another wheel: GrpcGateway
- PanGu-Coder: Function-level code generation model
- 数字量输入输出模块DAM-5088
- Matlab基础(4)——矩阵
- Matlab基础(0)——命令行常用指令
猜你喜欢
域名怎么注册备案解析?
Performance testing of API Gateway APISIX on Google Cloud T2A and T2D
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
电流继电器JL-8GB/11/AC220V
Manage reading notes upward
The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust
Voltage relay HDY - vac - 1 A / 1-220
云原生应用的概念和云原生应用的 15 个特征
Verilog语法基础HDL Bits训练 07
概率论的学习整理4:全概率公式
随机推荐
The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust
Verilog语法基础HDL Bits训练 08
Matlab基础(4)——矩阵
UE5 GAS Study Notes Postscript 0
概率论的学习和整理--番外4: 关于各种平均数:算术平均数,几何平均数,调和平均数,以及加权平均数和平方平均数 (未完成)
电压继电器HDY-A/1-220VAC-1
pg_rewind 修复主备环境的时间线
ADC0808/9 signal acquisition developed by single chip microcomputer
嵌入式环境下并发控制与线程安全
概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
2022-07-29 顾宇佳 学习笔记 异常处理
HJY-F931A/YJ三相电压继电器
Manage reading notes upward
Redis master-slave replication
牛客-TOP101-BM42
Verilog grammar basics HDL Bits training 07
域名怎么注册备案解析?
EA中的业务对象和业务实体你分得清吗?
Based on the analysis of the acoustic channel cable tunnel positioning technology
Reverse linked list - iterative inversion method