these are some usage example i have collected on how you can use Object.assign() in ES6 when you are coding in angular and typescript [4697], Last Updated: Mon Jun 24, 2024
Angular2
Tue Jun 13, 2017
0 Comments
520 Visits
I am new to javascript ES6 and i wasnt sure how to use Object.assig(), i saw this youtube video this guy made and these are my notes:
let obj = {};
let taco = {food:'taco'};
Object.assign(obj, taco);
console.log(obj);
OUTPUT:
Object {food: "taco"}
############# COPY
let taco = {food:'taco'};
let tacoCopy = Object.assign({}, taco);
console.log(tacoCopy);
OUTPUT:
Object {food: "taco"}
############# COMBINE / MERGE
let saucyTaco = {food:'taco', sauce:'hot'};
let creamyTaco = {food:'taco', hasSourCream:true};
Object.assign(saucyTaco, creamyTaco);
console.log('saucyTaco:',saucyTaco);
console.log('creamyTaco:',creamyTaco);
OUTPUT:
saucyTaco: Object {food: "taco", sauce: "hot", hasSourCream: true}
creamyTaco: Object {food: "taco", hasSourCream: true}
NOTICE saucyTaco now hasSourCream
############# two different objects
let taco = {food:'taco'};
let burrito = {food:'burrito'};
Object.assign(taco, burrito);
console.log('taco:',taco);
console.log('burrito:',burrito);
OUTPUT:
taco: Object {food: "burrito"}
burrito: Object {food: "burrito"}
NOTICE: taco is now the same as burrito
with Object.assign() the properties of the right outmost object passes on to it will always take presedence, in this case, burrit was last
############# two different objects version 2
let taco = {food:'taco'};
let burrito = {food:'burrito'};
let tacoCopy = Object.assign({}, taco); // make a copy of taco
Object.assign(taco, burrito,tacoCopy);
console.log('taco:',taco);
console.log('burrito:',burrito);
OUTPUT:
taco: Object {food: "taco"}
burrito: Object {food: "burrito"}
NOTICE: taco is taco again because is the last object in the Object.assing()
############# you can add properties to object with Object.assing()
let taco = {food:'taco'};
Object.assign(taco, {hasSourCream:true}); // make a copy of taco
console.log('taco:',taco)
OUTPUT:
taco: Object {food: "taco", hasSourCream: true}
############# you can achieve the same with the following
let taco = {food:'taco'};
taco.hasSourCream = false;
console.log('taco:',taco);
OUTPUT:
taco: Object {food: "taco", hasSourCream: false}
############# COMPARE TWO OBJECTS IS THEY ARE THE SAME
let taco = {food:'taco'};
let tacoCopy = Object.assign(taco, {hasSourCream:true});
console.log(taco ===tacoCopy );
OUTPUT: true
############################5:24
let taco = {food:'taco', souce: 'hot'};
let burrito = {food:'burrito'};
let saucyBurrito = {};
Object.assign(taco,burrito,saucyBurrito);
console.log('taco',taco);
console.log('burrito',burrito);
console.log('saucyBurrito',saucyBurrito);
taco Object {food: "burrito", souce: "hot"}
burrito Object {food: "burrito"}
saucyBurrito Object {}
############################ the same as above but notice the diffrence in saucyBurrito Object {}
let taco = {food:'taco', souce: 'hot'};
let burrito = {food:'burrito'};
let saucyBurrito = {};
Object.assign(saucyBurrito,taco,burrito); // saucyBurrito first
console.log('taco',taco);
console.log('burrito',burrito);
console.log('saucyBurrito',saucyBurrito);
taco Object {food: "taco", souce: "hot"}
burrito Object {food: "burrito"}
saucyBurrito Object {food: "burrito", souce: "hot"}
############################ same results as above, but shorter
let taco = {food:'taco', souce: 'hot'};
let burrito = {food:'burrito'};
let saucyBurrito = Object.assign({},taco,burrito); // ONE LINE INSTEAD OF TWO
console.log('taco',taco);
console.log('burrito',burrito);
console.log('saucyBurrito',saucyBurrito);
SAME OUTPUT
taco Object {food: "taco", souce: "hot"}
burrito Object {food: "burrito"}
saucyBurrito Object {food: "burrito", souce: "hot"}
############################ 7:44 using Composition and Mixins
const saucyTaco = {food:'taco', sauce: 'hot'};
const edibleTaco = {
food: 'taco',
eat: function(){
console.log('Ate the', this.food);
}
}
const burrito = {food:'burrito'};
const edibleSaucyBurrito = Object.assign({},
saucyTaco,edibleTaco,burrito
);
console.log(edibleSaucyBurrito.sauce);
edibleSaucyBurrito.eat();
OUTPUT
hot
Ate the burrito