Javascript Methods

| | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

JavaScript has an Object class which has all kinds of methods we can use to manipulate these objects. In this article, we’ll take a look at the Object.assign () method and show how it’s used.

We remind you that an object is a data type in JavaScript, similar to dictionaries in Python, which have what we call key: value pairs. Let’s say we have an item, let’s call it Harry. It can have key: value pairs similar to this:

The rule of thumb for the theme in this situation is that the name of the property to the left of the colon is the key and the things to the right are the value. Most of the keys and values ‚Äã‚Äãare pretty straightforward - the only one that can be a bit risky is the last property: pet. The pet is always the key here. The value, however, is an object that has two other key pairs: value. This is what we call a nested object.

Object.assign (target,… sourceObjs);

There is an Object method called assign () which can be used to take one or more source objects and copy them to a target object. Let’s go straight to the code to see what it does:

Let’s expose the code together. Let’s start with a literal object called hpChar. This hpChar has a first name, last name, age, domicile and pet. This pet property is an object itself and has a few keys and values ‚Äã‚Äãby itself.

Next, we have our first instance of our method that we’re here to talk about. Object.assign accepts two or more parameters. The first is always the target object to which you want to assign values. In this case, it’s an empty object.

The second parameter is our object defined above. This is the source material from which we’ll take to assign to the target object (our first parameter).

The following line saves the result in the console:

Nothing much different, right ? This is because we assigned hpChar to an empty object - we basically cloned the object. What if it had to be done again, but by changing certain values? Instead of the empty object this time, let’s replace it with the hpChar object - that will be our goal this time. Let’s change some values ‚Äã‚Äãon the second parameter. Do not hesitate to play with the values ‚Äã‚Äãas long as you keep the same keys.

Here are the values ‚Äã‚ÄãI will replace. If the value in the target object is not to be changed, it is not necessary to explicitly get its value in the source object.

The second time we saved the result to the console, it returned the target object, overwriting the properties with new values. Now our console looks like this:

This is what connects two objects with the same properties. What if we merged three objects with different properties? What do you hope will happen?

Obj1 is our obj target. Obj2 and obj3 are our source objects. These will merge into our target object. Since no properties overlap, all properties will be merged into obj1 with their respective values.

Here we go ! A working implementation of Object.assign (). Once you understand the concepts here, you are one step closer to learning a JavaScript framework !

Ready to learn more?

Check out these tutorials to deepen your study ...

JavaScript Object.keys ()

A step by step guide of the sheets JavaScript

JavaScript types

The JavaScript chain contains: Step by step guide

JavaScript Variables

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Javascript Methods, check other ast Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Angelo Schteiner

California | 2023-01-27

Maybe there are another answers? What Javascript Methods exactly means?. Will get back tomorrow with feedback

Javier Wu

Rome | 2023-01-27

time Python module is always a bit confusing 😭 Javascript Methods is not the only problem I encountered. Will get back tomorrow with feedback

Ken Sikorski

Shanghai | 2023-01-27

I was preparing for my coding interview, thanks for clarifying this - Javascript Methods in Python is not the simplest one. Will get back tomorrow with feedback

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically