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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:20:55+00:00 2026-05-26T12:20:55+00:00

The following ways of writing a javascript function are equivalent. Maybe the first one

  • 0

The following ways of writing a javascript function are equivalent.

Maybe the first one is more clear.

Nevertheless, many programmers prefer the second way.

There are significant difference between the two ways for preferring the second-one?

First way:

Class.prototype.fn = function () {
        var obj = { 
            … 
        };

        return obj;
};

Second way:

Class.prototype.fn = function () {

        return {
            .........
          };
};
  • 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-26T12:20:55+00:00Added an answer on May 26, 2026 at 12:20 pm

    Unless you need to perform an operation on obj after creating it via the literal, there’s no difference and it’s just a subjective style preference. (Note that said use could be in the code, or during debugging; more below.)

    So for example, there’s a real practical difference here:

    Class.prototype.fn = function () {
            var obj = { 
                subordinate: {
                    foo: function() {
                        if (obj.flag) {
                            doOneThing();
                        }
                        else {
                            doSomethingElse();
                        }
                    }
                }
            };
    
            return obj;
    };
    

    There, you need the name, so that obj.subordinate.foo() works. (Not that I’d advocate doing this, but it’s an example of when there’s an objective rather than subjective distinction.) But barring needing to use it after initialization and before return, it’s just a subjective thing.

    Of course, that use need not necessarily be in the code. The form with the obj variable can be more useful when debugging, if you need to inspect what you’re returning before you return it.


    Perhaps going a bit off-piste here, but I think it’s related: In contrast to the examples in your question, there’s a real, practical, tangible difference between this:

    Class.prototype.foo = function () {
            … 
    };
    Class.prototype.bar = function () {
            … 
    };
    

    and this:

    (function() {
        function Class_foo() {
                … 
        }
        function Class_bar() {
                … 
        }
    
        Class.prototype.foo = Class_foo;
        Class.prototype.bar = Class_bar;
    })();
    

    …that difference being that in the former, the functions have no names (the properties referring to them do, but not the functions themselves). In the latter case, the functions have real names, which help your tools help you by showing you names in call stacks, lists of breakpoints, etc.

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

Sidebar

Related Questions

I am writing the following function the second time in unrelated projects (first was
Consider the following two ways of writing a loop in Java to see if
Which one of the following ways would you use in declaring Primary Keys by
Using jQuery, I can find table row in the following ways. Which one is
I'm writing a Javascript <-> C# bridge and ran into the following problem: There's
I am writing a webpage with the following structure: One section (table A) depends
I am writing a function decorator that will apply a conversion to the first
In open source projects I see the following two ways of writing specs: Specs
I can select all the distinct values in a column in the following ways:
What is the best way to copy a list? I know the following ways,

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.