Object.assign merges objects (ES6 new syntax) to solve bloated code

Object.assign merges objects with new syntax in ES6 to solve bloated code

1. Understand

The main problem solved by Object.assign is the problem of object merging

Object.assign(target,…sources) //target is the target object, followed by objects that need to be merged into target (can be multiple)

The advantage is: it solves the problem of bloated code. Like before, ES5 users will use assignment operations. This new syntax is simple.

2.Code

let Objectname={
    
    
		category1Id:"",
		category2Id:"",
		category3Id: "",
		categoryName: "",
		keyword: "",
		order: "",
		pageNo: 1,
		pageSize: 10,
		props: [],
		trademark: "",
}
let query={
    
    category1Id:"1",categoryName:"手机"}
let params={
    
    keyword:"华为"}
console.log(Object.assign(Objectname,query,params))  // 将后两个对象合并到第一个对象 返回第一个对象

3.Output

Insert image description here

4. Practical use

1. Listen for changes in routing parameters and change this.route.query and this.route.query and this.The two parameters r o u t e . q u e r y and th i s . route.params are merged into the data that needs to be initiated .

2. Monitor routing changes in real time through watch and initiate ajax requests

3. Please see my next article for detailed introduction.

Guess you like

Origin blog.csdn.net/m0_62496369/article/details/127841272