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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:22:59+00:00 2026-05-30T04:22:59+00:00

I was wondering whether there is any performance / advantage of writing javascript in

  • 0

I was wondering whether there is any performance / advantage of writing javascript in this format ?

var myFuncs = {
  var firstFun = function() {
    // do something
  },

  var secondFunc = function() {
    // do something
  },

  var thirdFunc = function() {
    // do something
  }
}

So they can be called like

myFuncs.firstFun();

I’m trying to understand how this is more advantageous [other than code readability] ?

  • 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-30T04:23:01+00:00Added an answer on May 30, 2026 at 4:23 am

    Function vs Object declaration

    You can’t use that particular syntax, the correct form is:

    var myFuncs = {
      firstFn: function () {},
      secondFn: function () {},
      ...
    };
    

    The advantage to writing functions within an object has to do with namespacing and context. If you wrote:

    var firstFn = function () {};
    -or-
    function firstFn() {}
    

    the function would be defined at window.firstFn. Adding the functions on myFuncs makes the functions accessible at window.myFuncs.firstFn. If you want your JavaScript to work with other scripts you wouldn’t want to have your foo function conflict with someone elses foo function:

    <script src="a.js">
    function foo() {...}
    </script>
    
    <script src="b.js">
    function foo() {...} //this script would overwrite the foo function in a.js
    </script>
    
    <script src="c.js">
    var bar = { //this script would be accessed at bar.foo()
      foo: function () {..}
    }
    </script>
    

    The calling context (this) of the function will also be different:

    function foo() {
      console.log(this); //window
    }
    var bar = {
      foo: function () {
        console.log(this); //bar object
      }
    }
    

    Closure Syntax

    What you may be getting confused with is the syntax for declaring functions within a closure:

    (function () {
      var foo = function () {...};
      foo();
    }());
    

    In this case the closure is used to prevent the function from polluting the global scope (window.foo will not be set). This allows multiple scripts to use the same function names without worrying about being overridden.


    OOP Syntax

    The object syntax is often used to define the prototype for a JavaScript “constructor”. In JS all functions can be called as a constructor simply by using the new keyword when the function is called:

    function foo() {...}
    var f = new foo(); //don't do it this way
    

    For readability/maintainability/consistency you should always name your constructor using PascalCase:

    function Foo() {...} //tells other developers this is a constructor
    function bar() {...} //tells other developers this is a function
    var f = new Foo();
    var b = bar();
    

    Without getting too lost in the details of how prototype works, you can assign methods to be shared across every instantiated object of a function by assigning an object to the function’s prototype property:

    function Foo() {...}
    Foo.prototype = { //note the use of the object declaration for the functions
      bar: function () {...},
      baz: function () {...},
      ...
    };
    var f = new Foo();
    f.bar(); //calls the bar function that was defined in the prototype
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering whether there is any associated performance problems with using only one
I was wondering whether there was any advantage to clamping the angle passed to
I'm fairly new to the STL, so I was wondering whether there are any
I was just wondering whether there is some way to do this: I have
I'm wondering whether something like this is possible (and relatively easy to do), and
I am wondering whether there is any way to achieve partial JSP rendering in
I am wondering whether there is any difference between inlining functions on a linker
I am just wondering whether there is any work regarding the generation of static
I've just started playing with F#, and was wondering whether there is any recommended
I was wondering whether there are any pre-agreed values, coming from the old days

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.