1

This question already has an answer here:

Despite sharing a similar name and syntax, Java and JavaScript are quite different. However, they both have Object Oriented features.

As a JavaScript novice, the main differences that I can see between Java and JavaScript's Object Orientation is in inheritance and "class"/object description syntax.

Assuming the above is correct, would thinking about my JavaScript objects as Java objects while designing and subsequently "translating" them to JavaScript code be a good idea? My thinking is that both languages' respective objects boil down to properties and methods/functions. I don't plan to use inheritance or dynamically add properties/functions.

Would taking this approach lead to difficulties or miss major features of JavaScript?

marked as duplicate by gnat, Doc Brown, GlenH7, user22815, durron597 May 22 '15 at 0:51

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.

  • @gnat That's a great answer to that question. I think the difference in this question is the focus on what problems one might run into from a beginner's perspective. If you read gnat's reference answer and have time to follow the references it will get you a great perspective on what you could be missing in js. – joshp May 21 '15 at 5:33
  • 1
    MDN's Details of the object model article has a side by side comparison of basic OOP in Javascript & Java. – yannis May 21 '15 at 10:03
2

For the most part, you can write very Java-like code in JavaScript, but it's usually simpler not to. There are two pretty major factors that affect your design: dynamic typing and functional-style callbacks.

Dynamic typing means you don't use interfaces, and you don't need to create a lot of those little classes that do nothing else but implement interfaces or create glue between other classes or other things done to make static type systems happy. People who have only programmed in Java previously often don't realize just how much of it is type system boilerplate. Java is pretty bad in that department, even compared to other statically-typed languages.

Functional-style callbacks means instead of creating an object with an actionPerformed method, you just pass a function straight in. This eliminates a lot of the classes you create for the sole purpose of receiving asynchronous events, and gives you a lot more flexibility in what data gets passed back to you.

What you'll find is that JavaScript data model objects tend to be fairly similar to their Java counterparts, but control and glue logic usually is very different.

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