Objects and its internal representation in JavaScript

Every Object Oriented Programming has its upper hand because of its characteristic to represent a real world problem. This is being possible because of this object.

So, What is Object? Any real world entity can be considered as an object. For Example: car, mobile, laptop, headset etc.. All these things are considered to be an object. For better understanding lets take a fixed object throughout this blog. Since I like Elephants so much, I will be considering it throughout this example.

So every object has its properties and in JavaScript the object is represented using {}.

Example 1:

var Elephant={‘name’: ’Manickam’, ‘color’: ’Black’, ‘weight’:1800, ‘height’:9.8, ‘domain’: ’Eukaryota’, ‘country’: ’Asia’};

Here the properties is given as Key: Value pair. Keys and values are case- sensitive. An Object can have a number of data types inside it as values. It can have numbers, string, arrays and even another Object inside it.

This above example is a static version of the Object. Let us consider you want various elephants with same properties but the value might differ. We cant keep on typing so many lines of code for that, it will become messy. So there is a way to reuse the object structure in JavaScript.

If we want to add an extra property, its so simple just object_name.property_name=property_value. Here Elephant.play_Football=true;

Example 2:

function Elephant(name, color, weight, height, domain, country{

this. name= name;

this. color= color;

this. weight= weight;

this. height= height;

this. domain= domain;

this. country= country;

}

Using this Structure we can easily create n number of cute elephants.

var elephant1=new Elephant(‘Manickam’, ‘Black’, 1800, 9.1, ‘Eukaryota’, ‘Asia’);

var elephant2=new Elephant(‘Komban’, ‘Black’, 3600, 9.1, ‘Eukaryota’, ‘Asia’);

What if we want to add an extra property here? Its again so simple. Just add it using prototype keyword.

Elephant. prototype. play_Football=true;

This will make the play_Football property available to all the Objects created from the Constructor “Elephant”. Note that the “prototype” assigns the property as an internal property of the Object.

If we want to print elephant1 in console, this play_Football property wont be printed, but it is considered as an internal property. This property is assigned to the Whole Elephant objects instead of particular elephant1 or elephant2. If we want to access that we can use elephant1.play_Football.

Arrays, functions are also Objects in JavaScript. Even a prototype is an Object. As I said earlier, Objects can be associated with other Objects in JavaScript. Just like the prototype being associated with the Constructors and even in Arrays. If you provide a property to Array as a prototype it will be associated with all the Arrays.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aarthi K

Hi All! I am Aarthi, a Software developer by profession and a budding technical blogger. I am here to feed my tech thirst 😁.