Object Methods in JavaScript every developer needs to know

Tram Ho

This is an article in the series “tips and tricks javascript”

What is javascript object? In fact almost everything in javascript is an object. But in this post, we will learn which method objects are used the most and most widely, which every project must use. Besides, there are also “Array methods in javascript”.

What is javascript object?

To fully understand this article, you must first understand “What is an javascript object?” and above all you have to understand how to create, modifying, and working an object. In the previous tutorial of tipjs, talked a lot about Objects you can check out. In the previous “Array method in javascript” we talked a lot about how to use the Array method.

The javascript object is a collection of key/value . The values ​​may include properties and methods and may contain all other types of JavaScript data, such as String , Number and Booleans .

Objects have many useful integrated methods that we can use and access to simply work with individual Objects. The tutorial of this post will go through important integrated Object methods, and is accompanied by examples that illustrate each specific case when using the object.

Object.create ()

Object.create() is a method used to create a new Object and use that object to extend more to an object, let’s look at an example below.

Above that we also see that we can change the value of a properties of the new Object we have just used Object.create() .

Object.keys ()

Object.keys() is a method used to create an Array with all the keys of an Object. And from the experience of tipjs, maybe this is a very good method. Because tipjs are used a lot.

Once we have an Array from using Object.keys() , we can continue to use Method Array in javascript for further development like iterate:

Object.values ​​()

Object.values() is a method in contrast to Object.keys() that creates a new Array with all the values ​​of an object.

Object.entries ()

Object.entries() is a method that creates a nested array with the key / value of an Object.

Object.assign ()

Object.assign() is a method used to copy values ​​from one object to another. In the example below, we use Object.assign() to merge them together:

But the important thing is when using Object.assign() it is a shallow-cloning. See also “shallow-cloning in javascript” . In addition, if you merge an object, we can use (...) , see the example if you are interested:

And spread syntax is also a shallow cloning. See also The difference between Shallow copying and Deep copying in javascript object

Object.freeze ()

Object.freeze() used to prevent an act of changing the value attribute of an object, in addition to preventing an action such as deleting or adding properties.

To understand Object.freeze() not an example of this, you can understand the suggested tipjs you can read more about this article: How can I deep freeze an object in JavaScript?

Object.seal ()

Object.seal() is a bit opposite of Object.freeze() which is used to prevent the behavior of adding a new property but allowing modification of existing properties. For example:

At this point, we recommend that you continue to see more articles on “Differences Object.freeze () and Object.seal () in JavaScript”

Conclusion and draw lessons

The best thing is to give you a way to see, understand and use method objects in javascript effectively and subtly. For each example, we try to pass on articles that go further. Do not ignore it wastefully. Because each article contains more knowledge. In addition, you should also learn about “Array method in javascript” .

If you are interested in articles about javascript please follow: Tips and tricks javasccript

Further reference: digitalocean.com

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo