Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:17:24+00:00 2026-05-25T02:17:24+00:00

I’m trying to port an application I wrote in java to javascript (actually using

  • 0

I’m trying to port an application I wrote in java to javascript (actually using coffeescript).

Now, I’m feeling lost.. what do you suggest to do to create class properties? Should I use getter/setters? I don’t like to do this:

myObj.prop = "hello"

because I could use non existing properties, and it would be easy to mispell something..

How can I get javascript to be a bit more like java, with private, public final properties etc..? Any suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:17:25+00:00Added an answer on May 25, 2026 at 2:17 am

    If you just translate your Java code into JavaScript, you’re going to be constantly fighting JavaScript’s object model, which is prototype-based, not class-based. There are no private properties on objects, no final properties unless you’re using an ES5-compatible engine (you haven’t mentioned what your target runtime environment is; browsers aren’t use ES5-compatible, it’ll be another couple of years), no classes at all in fact.

    Instead, I recommend you thoroughly brief yourself on how object orientation actually works in JavaScript, and then build your application fully embracing how JavaScript does it. This is non-trivial, but rewarding.

    Some articles that may be of use. I start with closures because really understanding closures is absolutely essential to writing JavaScript, and most "private member" solutions rely on closures. Then I refer to a couple of articles by Douglas Crockford. Crockford is required reading if you’re going to work in JavaScript, even if you end up disagreeing with some of his conclusions. Then I point to a couple of articles specifically addressing doing class-like things.

    • Closures are not complicated – Me
    • Prototypical inheritance in JavaScript – Crockford
    • Private Members in JavaScript – Crockford
    • Simple, Efficient Supercalls in JavaScript – Me Includes syntactic sugar to make it easier to set up hierarchies of objects (it uses class-based terminology, but actually it’s just prototypical inheritance), including calling "superclass" methods.
    • Private Members in JavaScript – Me Listing Crockford’s solution and others
    • Mythical Methods – Me
    • You must remember this – Me

    Addressing some of your specific questions:

    what do you suggest to do to create class properties? Should I use getter/setters? I don’t like to do this:

    myObj.prop = "hello"

    because I could use non existing properties, and it would be easy to mispell something..

    I don’t, I prefer using TDD to ensure that if I do have a typo, it gets revealed in testing. (A good code-completing editor will also be helpful here, though really good JavaScript code-completing editors are thin on the ground.) But you’re right that getters and setters in the Java sense (methods like getFoo and setFoo) would make it more obvious when you’re creating/accessing a property that you haven’t defined in advance (e.g., through a typo) by causing a runtime error, calling a function that doesn’t exist. (I say "in the Java sense" because JavaScript as of ES5 has a different kind of "getters" and "setters" that are transparent and wouldn’t help with that.) So that’s an argument for using them. If you do, you might look at using Google’s Closure compiler for release builds, as it will inline them.

    How can I get javascript to be a bit more like java, with private…

    I’ve linked Crockford’s article on private members, and my own which lists other ways. The very basic explanation of the Crockford model is: You use a variable in the context created by the call to your constructor function and a function created within that context (a closure) that has access to it, rather than an object property:

    function Foo() {
        var bar;
    
        function Foo_setBar(b) {
            bar = b;
        }
        function Foo_getBar() {
            return bar;
        }
    
        this.setBar = Foo_setBar;
        this.getBar = Foo_getBar;
    }
    

    bar is not an object property, but the functions defined in the context with it have an enduring reference to it. This is totally fine if you’re going to have a smallish number of Foo objects. If you’re going to have thousands of Foo objects you might want to reconsider, because each and every Foo object has its own two functions (really genuinely different Function instances) for Foo_getBar and Foo_setBar.

    You’ll frequently see the above written like this:

    function Foo() {
        var bar;
    
        this.setBar = function(b) {
            bar = b;
        };
        this.getBar = function() {
            return bar;
        };
    }
    

    Yes, it’s briefer, but now the functions don’t have names, and giving your functions names helps your tools help you.

    How can I get javascript to be a bit more like java, with…public final properties

    You can define a Java-style getter with no setter. Or if your target environment will be ES5-compliant (again, browsers aren’t yet, it’ll be another couple of years), you could use the new Object.defineProperty feature that allows you to set properties that cannot be written to.

    But my main point is to embrace the language and environment in which you’re working. Learn it well, and you’ll find that different patterns apply than in Java. Both are great languages (I use them both a lot), but they work differently and lead to different solutions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.