How JavaScript uses the Object.assign function
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!