JavaScript Basics: Object.assign

in javascript •  7 years ago 

The function Object.assign is used to assign the values of “enumerable” and “own” properties from one or more source objects to a target object (first argument). If the properties have getters or setters, they will be invoked. This is what the word “assign” means, so it's not just copying values from an object to another.

Here is an example:

    class Test {
        constructor(arg) {
            Object.assign(this, arg);
        }
    }

In fact, this is my favorite use case for this operation. The above code gives you the power to assign any properties of the newly created object in the constructor.

Remember that the function can accept multiple source objects as further arguments. This function overwrites any values that already exist in the target object.


Related Posts

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

great as always

Thanks for the post. It's helping me to do my work.

i learn many thing from your post

  ·  7 years ago (edited)

Es un buen post, muy interesante y nos ayuda mucho a nosotros los nuevos, pasa por mi blog y apoyame

Good post on assigning object.

this is very informative blog .thanks to you for sharing..i praise
you..
Carry on
thanks for sharing

Really helpful post

I like

nice post.. i like your post

thanks for the valuable post
resteemit done

you are great soul

i learn many things from your post... thanks for share...