1

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?

2
  • @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
    CommentedMay 21, 2015 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
    CommentedMay 21, 2015 at 10:03

1 Answer 1

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.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.