Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

JavaScript is a prototype language. To turn it into a class based language adds little value? I am not talking about best-practice here. I remember reading an article from way back, which claimed that the class-based worldview is perceivably more flawed than the one of prototypes. My summary can be found here: http://carnotaurus.tumblr.com/post/3248631891/class-based-javascript-or-not. I am resisting to use the class-based jQuery add-on and other attempts at faciliating class-based JavaScript. Peer pressure is strong but is there a stronger theoretical or practical reason why I should stop resisting?

share|improve this question

migrated from stackoverflow.com Feb 15 '11 at 22:03

This question came from our site for professional and enthusiast programmers.

    
i don't know...why did u stop resisting? perhaps Mootools can help –  kjy112 Feb 15 '11 at 18:54
3  
I was under the impression this language change is done primarily because many developers don't grasp the intricacies of prototypical object-orientation. And maybe a class-based model can more rigidly restrict the inheritance tree. –  mario Feb 15 '11 at 22:09
    
@mario Sounds about right to me –  Carnotaurus Jan 1 '12 at 10:57

3 Answers 3

up vote 7 down vote accepted

Continue to resist!

JavaScript is fine as is, it just suffers from a few misunderstandings.

  1. It's not Java. It's not even related. The name was chosen to ride on Java's coat tails and it succeeded but fostered the idea that it was somehow a lightweight version of Java.
  2. It's functional. Functions are "first-class citizens". Being comfortable with functional programming will give you a leg up on JavaScript.
  3. It's prototype based. This means you don't have classical inheritance but this gives you greater flexibility to mix and match.
share|improve this answer
    
Regarding your point #1, it's actually the other way round: Sun paid Netscape to change the name because they wanted Java to ride LiveScript's coat tails, not the other way round. Java in the browser was a failure, even back then, there simply was no coat tail to ride for Netscape ;-) –  Jörg W Mittag Aug 18 '12 at 12:54

Recently I discovered the reasoning behind prototype based versus class based object oriented languages. I always thought that prototypes make no sense, because sooner or later you will find out your objects are in some way similar and you can group them, create hierarchies and use polymorphism... I think the point is to understand, that with prototypes, the language itself is more flexible - you could for instance implement contracts, aspect oriented programming and all sorts of metaprogramming much easier. You could create smarter interfaces, components with dependency resolution and much more... I think prorotypes are a bettter language feature, but one has to remember, that classes are needed and there should be a library for them.

share|improve this answer

You shouldn't care whether your JavaScript is class based or prototype based. The important question is how to make your program object oriented. Since the DOM plays the central role in websites/web-applications you could focus on making the DOM nodes object oriented instead. See for example this demo (implemented using the Entwine library):

http://jsfiddle.net/4Yacx/3/

share|improve this answer
    
JavaScript is already object oriented... I'll have a play around with this UI in a bit. I can swap different jQuery libraries in and out? It looks fun. –  Carnotaurus Feb 15 '11 at 19:16
1  
JavaScript is a language that enables you to implement a website/web-application in an object oriented way. But writing in JavaScript does not automatically make your program object oriented. –  Erik Feb 15 '11 at 19:21
    
And yes you can swap libraries in and out using jsfiddle. –  Erik Feb 15 '11 at 19:22
    
Also Alan Kay (who coined the term 'Object-oriented') has the following to say about class vs. protype based OO: userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en –  Erik Feb 15 '11 at 19:40
2  
If you're only using the OO part of JS, as awesome as it may be, you're not using the whole language. No need to ignore that bit of functional programming you have available. –  delnan Feb 16 '11 at 14:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.