What is the usage of assign in es6
In es6, assign is used to merge objects. All enumerable properties of the source object can be copied to the target object; if the target object and the source object have properties with the same name, or multiple source objects have properties with the same name , then the later attributes will overwrite the previous attributes, and the syntax is "Object.assign(...)"
The operating environment of this tutorial: windows10 system, ECMAScript Version 6.0, Dell G3 computer.
What is the usage of assign in es6
The Object.assign method is used to merge objects, copying all the enumerable attributes (key:value) of the source object (source) to the target Object (target)
For example:
const target = { a : 1 }; const source1 = { b: 2 }; const source2 = { c: 3 }; Object.assign(target,source1,source2) // target { a:1, b:2, c:3 }
Note: If the target object and the source object have properties with the same name, or multiple source objects have properties with the same name, the later properties will overwrite the previous properties.
For example:
const target = {a:1,b:1}; const source1 = { b: 2,c:2}; const source2 = {c:3}; Object.assign(target,source1,source2); // target {a:1,b:2,c:3}
If there is only one parameter, Object.assign will directly return the parameter. If the parameter is not an object, the object will be transferred out first and then returned. Since null and undefined cannot be converted are objects, so an error will be reported if they are used as parameters. If a non-object parameter appears in the position of the source object (meaning it is not the first parameter), then the processing rules are different. First, these parameters will be converted into objects. If null or undefined appears, as long as it is not the first parameter, no error will be reported.
For example:
const obj = {a:1}, Object.assign(obj) === obj // true typeof Object.assign(2) // object Object.assign(undefined) // 报错 Object.assign(null) // 报错 Object.assign(obj,undefined)
Other types of values (i.e. numeric values, strings and Boolean values) are not included in the first parameter and no error will be reported, but the string will be copied into the target in the form of an array. Object, other values will have no effect.
const v1 = 'abc'; const v2 = true; const v3 = 10; const objCurrent = Object.assign({},v1,v2,v3); // {0:'a',1:'b',2:'c'}; // 上面代码中,v1,v2,v3分别是字符串、布尔值、数值,结果只有字符串符合目标对象(以字符串数组的形式),数值与布尔值都会被忽略。这是因为只有字符串的包装对象,会产生枚举属性。 Object.assign()的深浅拷贝问题 const obj1 = {a: 1}; const obj2 = {b: 2}; const obj3 = {c: 3}; const obj = Object.assign(obj1,obj2,obj3); console.log(obj); // {a:1,b:2,c:3} console.log(obj1); // {a:1,b:2,c:3} 原始对象也被改变啦 const v1 ={a:1}, const currentObj = Object.assign(JSON.parse(JSON.stringify(v1)),{e:2}) console.log(currentObj) // {a:1,e:2} console.log(v1) // {a:1} 并没有发生变化
When the object has only first-level attributes and no second-level attributes, this method is a deep copy. However, when there are objects in the object, this method is a shallow copy after the second-level attributes.
Use recursion to implement deep copy
// _deep 深度拷贝的方法 function _deep(source){ let target; if (typeof source === 'object'){ // 判断目标值是数组还是对象 target = Array.isArray(source) ? []: {} for (let key in source) { // 指示对象自身属性中是否含有指定的属性 if(source.hasOwnProperty[key]){ // 如果属性不是对象则赋值,负责递归 if(typeof source[key] !== 'object'){ target[key] = source[key] }else { target[key] = _deep(source[key]) } } } } else { target = source } // 返回目标值 return target }
Use js to implement deep copy
function _deepJs(_data){ return JSON.parse(JSON.stringify(_data)); }
[Related recommendations: javascript video tutorial, web front end】
The above is the detailed content of What is the usage of assign in es6. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



async is es7. async and await are new additions to ES7 and are solutions for asynchronous operations; async/await can be said to be syntactic sugar for co modules and generator functions, solving js asynchronous code with clearer semantics. As the name suggests, async means "asynchronous". Async is used to declare that a function is asynchronous; there is a strict rule between async and await. Both cannot be separated from each other, and await can only be written in async functions.

In ES6, you can use the reverse() method of the array object to achieve array reversal. This method is used to reverse the order of the elements in the array, putting the last element first and the first element last. The syntax "array.reverse()". The reverse() method will modify the original array. If you do not want to modify it, you need to use it with the expansion operator "...", and the syntax is "[...array].reverse()".

For browser compatibility. As a new specification for JS, ES6 adds a lot of new syntax and API. However, modern browsers do not have high support for the new features of ES6, so ES6 code needs to be converted to ES5 code. In the WeChat web developer tools, babel is used by default to convert the developer's ES6 syntax code into ES5 code that is well supported by all three terminals, helping developers solve development problems caused by different environments; only in the project Just configure and check the "ES6 to ES5" option.

Steps: 1. Convert the two arrays to set types respectively, with the syntax "newA=new Set(a);newB=new Set(b);"; 2. Use has() and filter() to find the difference set, with the syntax " new Set([...newA].filter(x =>!newB.has(x)))", the difference set elements will be included in a set collection and returned; 3. Use Array.from to convert the set into an array Type, syntax "Array.from(collection)".

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

In es6, the temporary dead zone is a syntax error, which refers to the let and const commands that make the block form a closed scope. Within a code block, before a variable is declared using the let/const command, the variable is unavailable and belongs to the variable's "dead zone" before the variable is declared; this is syntactically called a "temporary dead zone". ES6 stipulates that variable promotion does not occur in temporary dead zones and let and const statements, mainly to reduce runtime errors and prevent the variable from being used before it is declared, resulting in unexpected behavior.

No, require is the modular syntax of the CommonJS specification; and the modular syntax of the es6 specification is import. require is loaded at runtime, and import is loaded at compile time; require can be written anywhere in the code, import can only be written at the top of the file and cannot be used in conditional statements or function scopes; module attributes are introduced only when require is run. Therefore, the performance is relatively low. The properties of the module introduced during import compilation have slightly higher performance.

The map is ordered. The map type in ES6 is an ordered list that stores many key-value pairs. The key names and corresponding values support all data types; the equivalence of key names is determined by calling the "Objext.is()" method. Implemented, so the number 5 and the string "5" will be judged as two types, and can appear in the program as two independent keys.
