views:

185

answers:

5

A friend and I had an argument last week. He stated there were no such things as classes in Javascript.

I said there was as you can say var object = new Object()

he says "as there is no word class used. Its not a class.

-- Whats your take on it guys?

thanks.

+2  A: 

By "language X has classes" people usually mean support of object oriented programming.

Yes, Javascript is an object oriented language.

Pavel Radzivilovsky
+5  A: 

In Javascript pretty much everything is an object (objects can inherit from other objects). It does not have classes in the classical sense.

Although you can reproduce most of the functionality of traditional class definition / instantiation by function prototyping.

ChristopheD
"In Javascript everything is an object": not true. There are also primitive values, like `undefined`, `null`, etc.
Marcel Korpel
@Marcel Korpel. Yes, that's correct (will edit my answer)
ChristopheD
void? does void exist in javascript - I'm as3 guy see... I've never seen void in js.
Glycerine
A: 

AFAIK Javascript use the prototype concept and it's not OO. That's means that you can't use the typical concepts of OOP like inheritance or polymorphism.

Cesar
JavaScript is a prototype-based programming language, which means it *is* an object-oriented language.
Steve Harrison
I love this site because there is always something to learn ;)
Cesar
Both inheritance and polymorphism can be applied in Javascript.
Matt
+16  A: 

Technically, the statement "JavaScript has no classes" is correct.

Although JavaScript is object-oriented language, it isn't a class-based language—it's a prototype-based language. There are differences between these two approaches, but since it is possible to use JavaScript like a class-based language, many people (including myself) often simply refer to the constructor functions as "classes".

Steve Harrison
You can simulate classes using prototypes and prototypes using classes, e.g. look at he Prototype design pattern.
Gabriel Ščerbák
I think I've earned my fiver! Nice one steve
Glycerine
A: 

Listen to Douglas Crockford's talk here:
http://developer.yahoo.com/yui/theater/video.php?v=crockonjs-2

He directly addresses your question in his presentation:

The most controversial feature of the language is the way it does inheritance, which is radically different than virtually all other modern languages. Most languages use classes – I call them ‘classical languages’ – JavaScript does not. JavaScript is class free. It uses prototypes. For people who are classically trained who look at the language, they go: well, this is deficient. You don’t have classes, how can you get anything done? How can you have any confidence that the structure of your program’s going to work? And they never get past that. But it turns out…

Christopher Altman