JavaScript Basics: Object.assign

in #javascript6 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

Sort:  

great as always

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

i learn many thing from your post

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

nice post.. i like your post

thanks for the valuable post
resteemit done

Coin Marketplace

STEEM 0.21
TRX 0.14
JST 0.030
BTC 69500.15
ETH 3394.50
USDT 1.00
SBD 2.75