Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IS OOP possible in Javascript?

Tags:

javascript

I recently found out that Javascript function can have classes, so I was wondering if OOP is also possible through javascript. Is It? If yes, Could you please point out some tutorials or site, where I can start with?

like image 724
Starx Avatar asked Aug 21 '10 02:08

Starx


2 Answers

OOP is definitely possible. While Javascript doesn't have "classes" like most OO languages do, what it does have is called "prototypes". Basically, objects are defined in terms of other objects rather than classes. (Objects can also emulate classes to some degree, for those who can't wrap their minds around prototypal inheritance.)

One could argue JS's OO capabilities exceed those of most languages, considering objects play an even more essential role than in languages with classes.

like image 111
cHao Avatar answered Sep 22 '22 06:09

cHao


OOP is central to Javascript, but it's not classical OOP. Javascript uses prototypes, not classes.

Douglas Crockford is a Javascript genius, so his Prototypal Inheritance in Javascript is a nice starting point. A Google search for "Javascript OOP" likely will turn up some neat articles to peruse, as well — I like the article by Mike Koss.

like image 45
Matchu Avatar answered Sep 23 '22 06:09

Matchu