How Dieting Can Affect Our Mental Health?

What we eat has a profound impact on our physical & mental wellbeing. Commercial diets can have a decidedly negative effect on our mental health and can even exacerbate current mental health…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




An Introduction to the internal representation of Objects in JavaScript

This article is for people with a basic understanding of JavaScript language and wants to know-how Objects work in JS. We will take a closer look at their internal representation.

So there’s a saying in JavaScript which says that “Everything is an object.”And while this is almost correct, it is not 100 percent correct though. Because in JavaScript, we have two big types of values, primitives, and objects. And primitives are all the values that have one of the data types that we already know about.So numbers, strings, booleans, undefined and null values, all of these are primitives,and everything else are objects.

And in fact, even simple things like functions, arrays, or dates are all objects in JavaScript, and that’s because in JavaScript objects are not just for storing a bunch of data, but for really complex things under the surface. So we have been using objects all along, just without knowing it. So Objects basically can store data in an unordered fashion with something called “key: value” pairs.

John Object with different “key: value” pairs as attributes.
John Object with “key: value” pairs

So in the given code snippet, you can see how an object is declared. The name of the object is John with “key: value” pairs as attributes. Here firstName is key and its respective value is “John”. Similarly lastName, age also keys and “Mayers”, 24 are respective values. The important takeaway is noticing how single John Object has different attributes with key-value pairs and we can store as many as we need.

Now that we know what the object is, it is crucial to access the items we stored in objects. And to do that we can access the values stored from the respective key values. These are called properties of an Object. So, console.log(John.firstName) will display the contents (values) associated with firstName property in John Object. Similarly, we can access all the other values from respective properties. You define (and create) a JavaScript object with an object literal.

We have now gained an overview of Objects, there creation and how to access those stored properties and property values. Objects can also have methods. Methods are actions that can be performed on objects. Methods are stored in properties as function definitions. Let's understand this with John’s example.

So here in John Object, we have created a method called fullName and what it will do is to return the full name of John i.e., John Mayers when a method is called. So we now know how to access an object method too. Congratulations!

In a function definition, this refers to the "owner" of the function. In the example above, we can use this keyword instead of John.firstName in the John object that "owns" the firstName property.

this Keyword: Instead of John.firstName we use this.firstName

Pro-Tip:

Add a comment

Related posts:

The Adventure of a lifetime

There are many things to do that can come to be adventurous. Simply going on vacations, hikes, and even skydiving. I personally have an all time favorite adventure. That adventure is scuba diving. I…

7 Consejos para Mejorar la Calidad del Brote de Cannabis

1. Comenzar con Top-Shelf Genética- obtenga los efectos y la apariencia que desea. ¡Su habilidad de cultivo marca una gran diferencia, pero una planta no puede superar sus genes! Si quiere cogollos…

Integrate Emotion Analysis into Your Skype Calls

You only get one chance to make a good first impression. Our clients always ask how exactly our emotion analysis tech works, so we’ve decided to conduct a little Christmas experiment and have a bit…