The Object.assign() Method in JavaScript - Set default options for functions

preview_player
Показать описание
The assign() method on the Object constructor in JavaScript is used to merge multiple objects together. You can use it to merge as many objects as you like.

If multiple objects have the same property, the "merged object" will also contain that property but the value of it will come from the last object passed in as an argument. This means it can be used to easily create "default options" within a function.

Support me on Patreon:

For your reference, check this out:

Follow me on Twitter @dcode!

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

oh this video hit gold as i was trying to understand a certain code

psyferinc.
Автор

Fantastic explanation, I was lost before.

KateKrauss
Автор

Great video. Kind of what I am looking for, but not exactly. You did good, and it is useful!

notsure
Автор

Good explanation with good example...👍😊

adarshkhatri
Автор

Great video, this is exactly what I needed!

azharali
Автор

Thank you for helping us writing nice and clean code ! :)

adelineiscoding
Автор

This reallyyy helped me to understand this. Thank you a billion!!! :))))

milkdromda
Автор

It should be noted that by the steps shown in the video, the original course object is edited as well to now include a score property.

justinus_stoicus
Автор

what if we have to Map over the properties of finalResult....how would be able to do that

naunihaldhillon
Автор

Best video on internet
about object.assign() :)
Thanks Dom = Doomer XD

SACHIN-gdzy
Автор

Mrs Water and Steve Bottle... lol imma guess u were thirsty bro

xesiusprime
Автор

If found out, that defaults is OVERWRITTEn.
So instead of:
options = Object.assign(objct1, objct2)
console.log(objct1)
//you can just write
Object.assign(objct1, options)
console.log(objct1)

Why would you (and everyone else) teach, that Object.assign passes something to options, while it seems, that actually Object.assign just overwrites the first object with the second object.

you can not use the first object more then once, since its overwritten with the assign method. nobody mentions this. its just "creating a new object", but ist not realy, righT?! :o

ElrondMcBong