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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:51:11+00:00 2026-06-02T04:51:11+00:00

I see objects in JavaScript organized most commonly in the below two fashions. Could

  • 0

I see objects in JavaScript organized most commonly in the below two fashions. Could someone please explain the difference and the benefits between the two? Are there cases where one is more appropriate to the other?

Really appreciate any clarification. Thanks a lot!

First:

   var SomeObject;

    SomeObject = (function() {

     function SomeObject() {}

         SomeObject.prototype.doSomething: function() {

         },
         SomeObject.prototype.doSomethingElse: function() {

         }

    })();

Second:

SomeObject = function() {

 SomeObject.prototype.doSomething: function() {

 },
 SomeObject.prototype.doSomethingElse: function() {

 }

}
  • 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-06-02T04:51:13+00:00Added an answer on June 2, 2026 at 4:51 am

    Both of those examples are incorrect. I think you meant:

    First:

    var SomeObject;
    SomeObject = (function() {
    
        function SomeObject() {
        }
    
        SomeObject.prototype.doSomething = function() {
        };
    
        SomeObject.prototype.doSomethingElse = function() {
        };
    
        return SomeObject;
    
    })();
    

    (Note the return at the end of the anonymous function, the use of = rather than :, and the semicolons to complete the function assignments.)

    Or possibly you meant:

    function SomeObject() {
    }
    
    SomeObject.prototype.doSomething = function() {
    };
    
    SomeObject.prototype.doSomethingElse = function() {
    };
    

    (No anonymous enclosing function.)

    Second:

    function SomeObject() {
    }
    SomeObject.prototype = {
    
        doSomething: function() {
        },
        doSomethingElse: function() {
        }
    };
    

    (Note that the assignment to the prototype is outside the SomeObject function; here, we use : because we’re inside an object initializer. And again we have the ; at the end to complete the assignment statement.)

    If I’m correct, there’s very little difference between them. Both of them create a SomeObject constructor function and add anonymous functions to its prototype. The second version replaces the SomeObject constructor function’s prototype with a completely new object (which I do not recommend), where the first one just augments the prototype that the SomeObject constructor function already has.

    A more useful form is this:

    var SomeObject;
    SomeObject = (function() {
    
        function SomeObject() {
        }
    
        SomeObject.prototype.doSomething = doSomething;
        function doSomething() {
    
        }
    
        SomeObject.prototype.doSomethingElse = doSomethingElse;
        function doSomethingElse()
        }
    
        return SomeObject;
    
    })();
    

    There, the functions we assign to doSomething and doSomethingElse have names, which is useful when you’re walking through code in a debugger (they’re shown in call stacks, lists of breakpoints, etc.). The anonymous function wrapping everything is there so that the doSomething and doSomethingElse names don’t pollute the enclosing namespace. More: Anonymouses anonymous

    Some of us take it further:

    var SomeObject;
    SomeObject = (function() {
        var p = SomeObject.prototype;
    
        function SomeObject() {
        }
    
        p.doSomething = SomeObject$doSomething;
        function SomeObject$doSomething() {
    
        }
    
        p.doSomethingElse = SomeObject$doSomethingElse;
        function SomeObject$doSomethingElse()
        }
    
        return SomeObject;
    
    })();
    

    …so that not only do we see doSomething, but SomeObject$doSomething in the lists. Sometimes that can get in the way, though, it’s a style choice. (Also note I used the anonymous function to enclose an alias for SomeObject.prototype, to make for less typing.)

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

Sidebar

Related Questions

I have two arrays of JavaScript Objects that I'd like to compare to see
Im having some issues creating an array of objects in javascript Please see my
So I'm trying to figure out how to compare two jQuery objects, to see
I've been trying to see if I can build my JavaScript objects as intuitively
I've come across two different ways to define/name objects and functions in JavaScript that
Inspired by this question . I commonly see people referring to JavaScript as a
I see some attributes of some objects in JavaScript start with double underscore. For
I often see web applications where a program is basically some javascript objects wrapping
In javascript on a browser, I can do this to see if an object
I'm not sure if this is possible but I want to see objects created

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.