Objects and its internal representation in Javascript

Vishnu Radhakrishnan O
4 min readNov 4, 2020

The object is an important data-type in and forms the building blocks for modern JavaScript. The object is a Composite data type and is quite different as compared to the primitive data types like Number, String, Boolean, null, undefined and symbol.

Objects are very complex and it contains any combination of these primitive data-types as well as reference data-types.
The object is a reference data type. Variables that are assigned a reference value are given a reference or a pointer to that value. That reference or pointer points to the location in memory where the object is stored.

The objects in JavaScript may be defined as an unordered collection of related data in the form of key-value pairs. the keys can be variable or functions and they are called properties and methods.respectively.

The Object can be created by using brackets {}. The property is a “key: value” pair, where the key is a string ( called a “property name”), and value can be anything.

let car = {
name:” Tata”,
location:” Kerala”,
manufacture_date:2000,
}

In this example the name, location, Manufacture date are so-called “keys” and Tata, Kerala,2000 are the values respectively.

Keys are referred to the properties of the object An object in JavaScript can have a function as a member, in such a case it will be known as a method of that object.

let cars = {
name:” Tata”,
location:”Kerala”,
manufacture_date:2000,
displayInfo : function()
{
console.log(cars.name,”was manufactured in”,cars.manufacture_date,”in”, cars.location);
}
};
cars.displayInfo();

Output:
Tata was manufactured in 2000 in Kerala

Here “displayInfo” is used as the method of the above object.

Properties

The Properties can be a string or a number. If it is a number, they can be accessed only by using bracket notation.

let cars = {
name :”Tata”,
location :”Kerala”,
manufacture_date:2000,
1 : 1000,
displayInfo : function()
{
console.log(“The value of 1 is”,cars[“1”]);
}
};
cars.displayInfo();

Output:
The value of 1 is 1000

Property names can be a strings with more than one space separated words. In that case, these property names must be enclosed in quotes

let cars = {
“car name” :”Tata”}

We can use split method to get a specific part of the value.

let cars = {
name :”Tata Nexon”,
location :”Kerala”,
manufacture_date:2000,
1 : 1000,
displayInfo : function()
{
console.log(cars.name.split(“ “)[1]);
}
};
cars.displayInfo();

Output:
Nexon

Object creation.

There are several syntaxes to create the object in Javascript. In the above codes, we have used Object literal syntax ie,{…}. Objects in JavaScript may also be created using the constructors, Object Constructor or the prototype pattern.

Object Constructor: The Object constructor in JS creates an object wrapper for the given value. This, used in conjunction with the “new” keyword allows us to initialize new objects.

const cars = new Object();
cars.name = “Tata”;
cars.location = “Delhi”;

cars.displayInfo = function(){
console.log(cars.name,”was in”,cars.location);
};
cars.displayInfo();

the above-mentioned method is suited to programs that require the creation of multiple objects of the same kind, as it would involve repeatedly writing the above lines of code for each such object. To avoid these problems we can use two other methods.

Constructors: Constructors in JavaScript, like in most other OOP languages, provides a template for the creation of objects. In other words, it sets properties and methods that would be common to all objects initialized using the constructor.

function Vehicle(name, brand) {
this.name = name;
this.brand = brand;
}
let car1 = new Vehicle(‘endeavour’, ‘Ford’);
let car2 = new Vehicle(‘city’, ‘Honda’)
console.log(car1.name); // Output: endeavour
console.log(car2.brand); // Output: Honda

By using the keyword “new” before the function turns it into a constructor. It creates a new object and sets the constructor property of the object to Vehicle.

We can use another method mentioned below

Inside classes, there can be special methods named constructor().
class cars {
constructor()
{
this.name = “Tata”;
}
}
let car1 = new cars();
// Output : Tata
console.log(car1.name);

Prototypes : An another way to create objects is using prototypes. JavaScript function has a prototype object property in default(it is empty). Methods or properties may be attached to this property.

Check this syntax
let obj = Object.create(prototype_object, propertiesObject)

// the second propertiesObject argument is optional
An example of making use of the Object.create() method is:
let cars = {
brand: “Tata”
}
let car1 = Object.create(cars);
// Output : Tata
console.log(car1.brand);
Output:
In the above example cars served as a prototype for creating the object “car1”.

Accessing the Object Members

Object members(properties or methods) can be accessed by using the
dot notation and bracket notation.
example
console.log(car1.name); // dot notation
console.log(cars1[‘name’]);//bracket notation

Unlike the dot notation, the bracket keyword works with any string combination, including, but not limited to multi-word strings.

car1.barnd name // not a valid syntax
cars1[‘brand name’] // valid

--

--