Define objects and their attributes with classes

Priti Jha
3 min readJul 10, 2023

--

You’ve probably heard the term object in a programming context before. But what does it really mean? Let’s start by looking at some real-world objects, like pens, books, smartphones, computers, etc.

You recognize all pens — fountain, ballpoint, felt-tip, gel, and so on — as being part of a type of object: pens. You can write with them, they use ink, and they can be held in one hand.

The same goes for books: they have a cover, a number of pages, a title, and one or more authors.

You notice commonalities between different objects, collect the information, and create a mental representation for a category of objects.

This mental list of attributes acts as a blueprint for that object. In programming, it's called a class. When creating a class, you can come up with any custom name, which is why they are called a named type. As you'll see, they also allow you to group lots of details together, which is why they can be referred to as complex types.

Before diving into classes, let's have a look at a complex JavaScript type: the object.

Object

JavaScript objects are written in JavaScript Object Notation (JSON) — a series of comma-separated key/value pairs between curly braces, which you can store in a variable:

let person={
"name": "Jhon",
"age": "39"
}

Each key is a string, and the associated values can be of any data type.

The major advantage of being able to build objects is that you can group the attributes of a single thing in one place.

Accessing an Object’s data

Now that you know how to create an object in JavaScript, let’s look at how to access the data within an object with dot notation:

let person={
"name": "Jhon",
"age": "39"
}

console.log(person.name);
// "Jhon"

Use the name of the variable containing the object, a dot (.), and then the name of the key for which you wish to retrieve the value.

Classes

Building an object by hand using curly-brace notation is fine for simple, single objects. However, you will often need many objects of the same type.

As mentioned earlier, a class is a blueprint for an object in code. It allows you to build multiple objects of the same type (called instances of the same class) more quickly, easily, and reliably.

Let's have a look at how you build a class in code:

To create a class in JavaScript, use the class keyword, followed by a name. Then enclose the class code in curly braces:

class person {}

constructor:

class person {
constructor(name, age){

}
}

There is a set of instructions to follow within the constructor when you create a new instance of the person class. To assign the name and age you receive to this instance, use this keyword and dot notation:

class person {
constructor(name, age){
this.name = name;
this.age = age;
}
}

Here, the this keyword refers to the new instance. Therefore, it is using dot notation to assign the received values to its corresponding keys.

Now that the class is complete, you can create new instances of it using the new keyword:

let newPerson = new person("abc", "32");

This line creates the following object:

{
name: "abc",
age: "32"
}

Having a person class allows you to create new person objects quickly and easily.

--

--

Priti Jha
Priti Jha

Written by Priti Jha

Senior front-end developer writing about Angular 8 , Ionic Framework ,Javascript, HTML, Css and all front end technology.

No responses yet