Objects in JavaScript and their Internal Representation

Objects in JavaScript and their Internal Representation

Objects | Object Properties | Object Methods } with example

First and foremost, never confuse JavaScript objects with other language object concepts; they are vastly different fundamentally. People who come from other languages, such as Java or C++, make the worst mistake by comparing JavaScript objects to others.

To get right to the point,

In plain English. "A JavaScript object is a named collection of values with state and behaviour (properties and methods)."

For example, a person, a car, a pen, a bicycle, a computer, a washing machine, and so on.

Consider automobiles.

Although all cars have the same attributes, the values of those properties vary from vehicle to vehicle. The processes are the similar in all cars, although they are carried out at different periods.

2020-mercedes-benz-c-class-angular-front-exterior-view_100745106_l.jpg

Let's have a look at the qualities (features) of my favourite Mercedes-Benz:

  1. Make: Mercedes
  2. Model: C-Class
  3. Color: White
  4. Fuel: Diesel
  5. Weight: 850kg
  6. Mileage: 8Kmpl
  7. Rating: 4.5

Using the preceding as a guide, I'll focus on objects, object properties, and methods.

# 1)Objects:

The following code creates a variable named vehicle and adds a basic value (Mercedes) to it:

var car = "Mercedes";

Objects, too, are variables. Objects, on the other hand, can hold a variety of values.

The following code sets a variable named Car to a variety of values (Mercedes, C-class, White, and so on).

var car = {Make: “Mercedes”, Model: “C-Class”, Color: “White”, Fuel: Diesel, Weight: “850kg”, Mileage: “8Kmpl”, Rating: 4.5};

Name:value pairs are used to represent the values (name and value separated by a colon).

Syntax:

var <object-name> = {key1: value1, key2: value2,... keyN: valueN};

"JavaScript objects are containers for named values," concludes and defines JS objects.

2)Object Properties

In JavaScript objects, the name:values pairs are referred to as properties.

var car = {Make: “Mercedes”, Model: “C-Class”, Color: “White”, Fuel: Diesel, Weight: “850kg”,Mileage: “8Kmpl”, Rating: 4.5};

Let's take a look at what fits under property and property value based on the above snippet:

1599324188785.jpg

Different primitive values, other objects, and functions can be used as object properties.

Although most properties can be updated, added, and deleted, others are read-only. The syntax for adding a property to an object is :

ObjectName.ObjectProperty = propertyValue;

The syntax for deleting a property from an object is:

delete ObjectName.ObjectProperty;

The syntax to access a property from an object is:

objectName.property        // Car.Make

           //or

objectName["property”]    // Car["Make"]

          //or


objectName[expression]   // x = "Make"; Car[x]

"Properties are the values associated with a JavaScript object," concludes the simple definition of Java Script properties.

3)Object Methods

An object method is a function definition contained in an object property.

i.e.,

Assume that there will be a mechanical function to start the car.

function(){return ignition.on}

Stop/brake/headlights on/off, etc. are all quite similar.

(I'm just trying to conjure up a mental image for you.) By using a code, the car will not start or stop. I'm assuming you know what I'm talking about).

Many examples and operations can also be found here.

"Methods are actions that can be done on objects," concludes the short description for Java Script Object methods.

I hope you find this beneficial.

Thank you very much.