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.

This question already has an answer here:

I am primarily a Java programmer. Because of its OO principles and the general paradigm of Java programming, like wrapping things in static variables, and having things return specific types, heavily aids me in "visualizing" a program. Instead of thinking of a big program, I can, instead, focus on smaller organized parts of my eventual program, and add functionality and build up from there.

Thus, I have trouble programming in other languages. Or at least, I have not been able to program in the same ability as I do in Java compared to other languages.

I know Javascript has OO principles, so I'd like to learn this language in a OO-based like I would program with Java. Is this possible?

share|improve this question

marked as duplicate by Jimmy Hoffa, World Engineer Oct 25 '13 at 21:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Some concepts carry over, but in general it is counterproductive and confusing to try to program one language in another. –  MichaelT Oct 25 '13 at 20:41
Java is class OO, JS is prototype OO, it just spawns off objects and you add attributes and objects inherit from other objects –  jozefg Oct 25 '13 at 20:44
Sorry - I am still a bit confused. Do programmers generally conceptualize in different programming paradigms? –  theGreenCabbage Oct 25 '13 at 20:50
Its best to program Javascript like javascript, otherwise you'll be constantly fighting against the grain. If you want something more OOP-y in the Java sense, you might look at TypeScript which is a more tradional OOP language that compiles to javascript. –  GrandmasterB Oct 25 '13 at 20:55
Do programmers conceptualize in different paradigms? Yes. Designing software or writing code in one language the same way as another rather than the way that language wants to be done yields poor results. –  Jimmy Hoffa Oct 25 '13 at 21:13

1 Answer 1

They are nearly opposites in terms of how they are arranged:

Javascript is a dynamic, weakly typed language that can be used in a functional manner fairly easily. It helps to think of everything in Javascript as being an Object, even the functions (since they technically are, kinda).

Objects are effectively dictionaries of variables (which in turn can be other objects) and functions (methods). These can be added on the fly, much as you could add a new key and value to a HashMap in Java. You can even change existing methods on the fly.

A prototype is effectively a property that is attached to an object (dictionary) that says "These are the things that you should already have initialized when you are created" and then has a list of those variables and methods that it has and that the new object will be created with. You can then add additional properties to that new object and further extend it down the line.

The weak typing will probably cause the most problems, just remember that when in doubt, use === rather than ==.

In a more general sense, you generally want to adopt the idioms and quirks of a new language as your own. Programming language X in language Y is usually a recipe for disaster down the road. Additional general points are effectively covered by this question.

share|improve this answer

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