- The Object.assign() method is used to copy the values of all enumerable properties from one or more source objects to a target object. it will return target object
- Object.assign(target, …sources) [target: target object], [souce: source object (multiple)]
Code example:
Properties in an object with the same key will be overwritten by the properties of the source object later
<template>
<view>
Object.assign()的基本使用
</view>
</template>
<script>
export default {
data() {
return {
obj: {
a: 1,
b: 2,
c: 3
},
obj1: {
c: 4,
d: 5,
}
}
},
onLoad() {
let obj3 = Object.assign({
}, this.obj, this.obj1)
console.log(obj3) // {a: 1, b: 2, c: 4, d: 5}
}
}
</script>
Code example:
If it is a string, it will be automatically converted to an object
<template>
<view>
Object.assign()的基本使用
</view>
</template>
<script>
export default {
data() {
return {
str : 'edg'
}
},
onLoad() {
let obj3 = Object.assign({
},this.str)
console.log(obj3) // {0: 'e', 1: 'd', 2: 'g'}
}
}
</script>