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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:53:39+00:00 2026-06-17T22:53:39+00:00

when read some javascript source, I found below code: var fullname = function(){ var

  • 0

when read some javascript source, I found below code:

var fullname = function(){
     var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
        return function(name){
          return shorts[name] || name;
      }
 }();

is it more effective or other reasons by returning another function?
why not use:

    function fullname(name){
         var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
         return shorts[name] || name; 
  }
  • 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-17T22:53:40+00:00Added an answer on June 17, 2026 at 10:53 pm

    This is equivalent to

    var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
    
    function fullname(name){
          return shorts[name] || name;
    }
    

    … in what it does. But

    var fullname = function(){
         var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
            return function(name){
              return shorts[name] || name;
          }
     }();
    

    … ensures that the hash/object shorts, is * private only to the function fullname*
    .

    So to answer yor question, Why not

      function fullname(name){
             var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
             return shorts[name] || name; 
      }
    

    Here’ shorts is hidden inside fullname, but as Dogbert points out, it’s slower
    because the hash shorts is being created at each invocation.

    But this gives the best of both worlds:

    var fullname = function(){
         var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
            return function(name){
              return shorts[name] || name;
          }
     }();
    

    shorts remains private to fullname
    and at the same time, shorts is only instantiated once, so performance will
    be good as well.

    This is what they call an IIFE (Immediately Invoked function expression). The idea is
    create a function A inside a function B and declare variables of use to function A
    within function B;s scope so that only function A can see them.

     var B = function(){
        // this is B
        // 
        val foo = {x:1}; // this is ONLY for A
    
        val A = function(arg0) {
           // do something with arg0, foo, and whatever
           // and maybe return something
        };
    
        return A;
     };
    
     val A = B();
    

    As you can see, that’s the same as

     var A = (function(){
        // this is in 'B'
        // 
        val foo = {x:1}; // this is ONLY for A
    
        return function(arg0) { // this is in A
           // do something with arg0, foo, and whatever
           // and maybe return something
        };
    
     })(/*calling B to get A! */);
    

    In functionality, this is exactly the same as

     val foo = {x:1};  // foo is visible OUTSIDE foo!
    
     val A = function(arg0) {
        // do something with arg0, foo, and whatever
        // and maybe return something**strong text**
     };
    

    I don’t see any other use.
    So it’s just a way of keeping variables private, and at the same time,
    not loosing performance.

    (see What is the (function() { } )() construct in JavaScript?)

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

Sidebar

Related Questions

Is there a way that a JavaScript function can read the JavaScript source code
I read some struts2 variable in javascript as follows: <javascript type=text/javascript> var data='<s:property value=simulationInfos/>';
for some reason this code wont work, it doesn't seem to read the javascript.php
I read some XSLT examples and found that code: <xsl:apply-template select=@*|node()/> What does that
I have some procedural javascript code that I have written for an open-source application
I just started to read some JavaScript project. Most of the .js file to
I have read through some tutorials about javascript prototypal inheritance patterns but I am
I need some assistance with some javascript/jquery I have put in place. The function
I'm a comer at JavaScript, and just learning some basic stuff (function, variable, etc)
I read some code recently that does something like this: bob = {'name': 'Bob

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.