The new method of ES6 object - object.assign ()

basic usage

1. The object.assign() method is used for merging objects, copying all enumerable properties of the source object (source) to the target object (target), both target and source belong to the containment relationship

Syntax: object.assign(target, source1,source2)

For example, if we want to jump from the home route to the search route, we can jump to the search route by clicking on the id of the first-level category, the id of the second-level category, the id of the third-level category, the name of the category, the search keyword, and jump to the search route. Once, it is necessary to assign the attribute value of the params or query parameter attribute to the attribute value of the corresponding SearchParams attribute. At this time, you need to use the object.assign() method to merge the attribute values ​​​​of the params and query parameter attributes into the attribute values ​​​​of the SearchParams attribute

2. Our SearchParams has ten attribute values 

3. This is the attribute value of the query and params parameter attributes

 

 4. You need to use the object.assign() method to assign the attribute values ​​​​of the query and params parameter attributes to SearchParams

important point:

1. If the target object and the source object have attributes with the same name, or multiple source objects have attributes with the same name, the latter attributes will override the former ones.

2. If there is only one parameter, Object.assign()it will be returned directly.

3. If the parameter is not an object, it will be converted into an object first and then returned.

4. Since undefinedand nullcannot be converted into objects, if they are used as parameters, an error will be reported.

5. If the non-object parameter appears in the position of the source object (that is, it is not the first parameter), then the processing rules are different. First of all, these parameters will be converted into objects, and if they cannot be converted into objects, they will be skipped. This means that if undefinedand nullis not the first parameter, no error will be reported.

 

Guess you like

Origin blog.csdn.net/weixin_46501763/article/details/128280126