August 11, 2017

[Code] Javascript Classes [Before ES6]

Yes, ES6 is here...yes now we can claim Javascript is on the way to OOPS as it has taken care of almost missing features and keywords. But still ES5 is very important and so is its concept.

Today I am going to tell you about the most confusing yet most important concept of Javascript - classes.

What is class?
Why it is important
Syntax of class
sample code
where to use?



What is class?

classes just like any other programming language do same in Javascript too. It gives your code:
1) Structure to your code
2) Blue print for your code
3) Re-use able code

Why it is important?

As mentioned above - class give your code a structure and it is like blueprint for your program. Helps in architecting your applications. With class it promote code re-useability too.


How to make class?





- Line #1  - we are creating 'flower' function with 3 parameters - name, color & price
- Line #2 - 4 -  Here we are declaring some properties & 'this' to refer whichever value will be passed when we will invoke the flower with the constructor.
- Line #5 - #7 - We are creating method here to do some calculations
- Line #10 - We are declaring 'Rose' constructor with 'flower' class with parameters

PS: Yes class in Javascript looks like function but unless and until you won't do Line #10 it won't work as class.

How to use it?


Now, lets call new constructor 


Where to use?You can make your own class such as - carousels, form validations, drop-down or anything and you can re-use them by customizing the properties and methods.
What's Next?Prototypes














No comments: