Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript uses the Object.assign function

Shulou Source: shulou.com Published: 2022-06-01 02:38:55 09月16日 Update

This article mainly introduces JavaScript how to use the Object.assign function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Object.assign: object attribute copy, shallow copy

Object.assign = Object.assign | | function () {if (arguments.length = = 0) throw new TypeError ('Cannot convert undefined or null to object'); let target = arguments [0], args = Array.prototype.slice.call (arguments, 1), key Args.forEach (function (item) {for (key in item) {item.hasOwnProperty (key) & & (target [key] = item [key])}) return target}

Use Object.assign to clone an object with money:

Let clone = Object.assign ({}, target)

Simple deep cloning can use JSON.parse () and JSON.stringify (), which parse json data, so only primitive types and arrays and objects other than symbol can be parsed.

Let clone = JSON.parse (JSON.stringify (target)) Thank you for reading this article carefully. I hope the article "how to use the Object.assign function in JavaScript" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Articles objects functions primitive two values interests at the same time properties copies data arrays more friends knowledge types editing industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Xiaomi MySQL Shulou Tech Info