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 7488919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:06:36+00:00 2026-05-29T15:06:36+00:00

I come from a Java/C++ OOP background and am trying to get into JavaScript

  • 0

I come from a Java/C++ OOP background and am trying to get into JavaScript “object-oriented” programming. I was looking through the source for a small framework and noticed something that I found odd. The framework defines some global functions, and then calls those functions from within the object’s functions. To clarify, here’s an example:

var MyObject = function() {

    function MyObject() {
        this.x = 5;
    }

    MyObject.prototype.getX = function() {
        return _MyObjectGetX( this );
    };

    return MyObject;
}();

var _MyObjectGetX = function( myObject ) {
    return myObject.x;
};

What I don’t understand is the usage of the global function. Let’s assume that global function is only used in one place: MyObject.getX(). Why not just move the return into the function body? Is this okay:

var MyObject = function() {

    function MyObject() {
        this.x = 5;
    }

    MyObject.prototype.getX = function() {
        return this.x;
    };

    return MyObject;
}();

This framework was written in CoffeeScript and then translated into JavaScript. Is this something that CoffeeScript does that may be unnecessary, or am I completely misunderstanding how JavaScript handles functions and objects (or rather, functions as objects)?

  • 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-29T15:06:38+00:00Added an answer on May 29, 2026 at 3:06 pm

    EDIT: sorry, misread your question.

    In this case it looks like coffeescript is just being dumb…sort of like wysiwyg generated html.

    var MyObject = function() {    
        function MyObject() {
            this.x = 5;
        }
    
        MyObject.prototype.getX = function() {
            return _MyObjectGetX( this ); // referencing a closure
        };
    
        return MyObject;
    }();
    
    var _MyObjectGetX = function( myObject ) {
        return myObject.x;
    };
    

    really has no advantage over the simpler

            var MyObject = function() {
                this.x = 5;
            };
    
            MyObject.prototype.getX = function() {
                return this.x;
            };
    

    And the outer getter function used in the top coffeescript example seems to have no gain over a simple return. In fact, neither really seems to have a legitimate use for the getter, as there’s no encapsulation being provided. The x property is readily available (and modifiable) on the object no matter where it is passed.

    There my be some reasons why the framework opted to do this…but for this simple example I don’t understand.

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

Sidebar

Related Questions

I come from a Java/AS3/Javascript background where all of my classes are organized into
I'm new to C programming, I come from a Java background. I was wondering
I come from a Java background and I am now trying to study PHP
I come from Java Background and so used to Debugging using Eclipse but have
I come from a Java background, where packages are used, not namespaces. I'm used
I come from a Java background and with any servlets-based technology, it's trivial to
I come from a java background. But I would like a cross-platform perspective on
I come from a Java background and have been using C# for the last
I've come from a java background, so i'm still getting to grips with some
I come from more of a Java background. In the last year or two,

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.