Tuesday, 17 November 2020

JavaScript Classes

Before we jump into JavaScript classes , Let's first know about classes. 

If you are a bit familiar with programming, you may know about variables. Variables are container in programming. In C programming, when you write, int i, you are saying the compiler that 'i' is a variable which you are going to use in your program latter. 

Same way, with another type of variables, strings, arrays, chars, floats, doubles etc. But when you work on real life, this primitive data type is not enough. You need much more. For example, you are going to store data of book or data of students or employees, which data type should you use? 

A book can have :

1 reference number 

2 Author 

3 publisher

4 price  etc. 


A student may have :

1. enrollment number

2. name 

3. department 

4. college/school 

5. grades 


An employee can have :

1. employee code 

2. department

3. salary etc. 


Which data type should you use here ? You may say make number of arrays to store a book's information or student's or employees' information. Of course, you can do it but obviously it's not an efficient way. It makes program complex and takes too much memory.  It's not time efficient too. Here classes and objects comes into the picture. 

Syntax to define classes and object may vary from one language to another. But background concept remains the same. 


Classes 

 - Class is one type of data type. 

 - It's a user defined data type. 

Example, 

When you need to store book information, you can make data type named 'book'. 

class Book

{

int refNo.

string name

string author

string publisher

float price

}

Here, refNo, name, author, publisher, price are properties of book. When you write this, there will be a variable in memory name book, which contains all these properties. 

memory representation when class is created

Now, make variable of type book.

book b1; 

Here, b1 is an object. Thus, object is a variable. 


Analogy with primitive data type. 

int i                   book b1

Here, int is datatype and i is variable. Similarly, book is data type and b1 is variable. 

If you want to access any property of b1 object, you can do like this : 

b1.refNo = 777888555

b1.name = "COSMOS"

b1.author = "Carl Sagan"

......

Now, we have adequate understanding of classes and objects, let's thrive into JavaScript classes. 


Basic syntax to define class : 

Use the keyword class

Use the keyword class to create a class.

Always add a method named constructor(): 

class MyClass { // class methods constructor() { ... } method1() { ... } method2() { ... } method3() { ... } ... }

Let's create a class named User.

class User { constructor(name) { this.name = name; }

sayHi() { alert(this.name); } } // Usage: let user = new User("duck"); user.sayHi();

Output  :                                                                                                            alert("duck")

Constructor method is automatically called  by New when we define object. So, we can initialize object there. 

If you don't define constructor method, JS will add an empty one. 


Class as a function 

In JavaScript, class is a kind of function. If you want to see this, try to use following code in above example, 

alert(typeof User); // function

You'll get function in alert box. 

These are some basic concepts of class. We'll discuss more advanced in latter article. 













No comments:

Post a Comment