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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:53:02+00:00 2026-05-23T12:53:02+00:00

var obj = { func: function() { return: { add: function() { } }

  • 0
    var obj = {

        func: function() {    
        return: {
           add: function() {
             }
          } 
        },
        somefunc: function() {
      }
   } 

The orginal code behind where i used to convert this…

var hash = (function() {
     var keys = {};
     return {         
     contains: function(key) {
     return keys[key] === true;         
     },
     add: function(key) { 
     if (keys[key] !== true){
         keys[key] = true;             
     }     
  }; 
})();

Questions:

  1. What is the use of return keyword?
  2. Can i structure like this, my class?
  • 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-23T12:53:03+00:00Added an answer on May 23, 2026 at 12:53 pm

    On the most basic level, the return keyword defines what a function should return. So if I have this function:

    function foo() { return 4; }
    

    And then call it:

    var bar = foo();
    

    foo() will return 4, hence now the value of bar is also 4.

    Onto your specific example:

    In this use case the return is used to basically limit outside access to variables inside the hash variable.

    Any function written like so:

    (function() {...})();
    

    Is self-invoking, which means it runs immediately. By setting the value of hash to a self-invoking function, it means that code gets run as soon as it can.

    That function then returns the following:

    return {         
        contains: function(key) {
            return keys[key] === true;         
        },
        add: function(key) { 
            if (keys[key] !== true){
                keys[key] = true;             
            }
        }     
    };
    

    This means we have access to both the contains and add function like so:

    hash.contains(key);
    hash.add(key);
    

    Inside hash, there is also a variable keys but this is not returned by the self-invoking function that hash is set to, hence we cannot access key outside of hash, so this wouldn’t work:

    hash.keys //would give undefined
    

    It’s essentially a way of structuring code that can be used to create private variables through the use of JavaScript closures. Have a look at this post for more information: http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-private-variables-in-javascript/

    Hope this Helps 🙂

    Jack.

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

Sidebar

Related Questions

Is it like... var obj = new Object(); obj.function1 = function(){ //code } or
Sometimes I'll see code like this: var Obj = Obj || {}; What does
$(#btnsub).click(function () { var add = $(#txtname).val(); var obj; var geo = new google.maps.Geocoder;
Is there an easier way to achieve the following? var obj = from row
var y = new w(); var x = y.z; y.z= function() { doOneThing(); x();
This query works great: var pageObject = (from op in db.ObjectPermissions join pg in
I use the AlphaImageLoader fix called supersleight with the following code: var supersleight =
In below sample code, I use lambda function to make 3 threads doing different
var obj = { '51' : { 'name':'name1'}, '66' : { 'name':'name2'}, '58' :
Let's asume that I have an object: var obj = {A:a, B:b, x:y, a:b}

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.