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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:09:17+00:00 2026-06-13T04:09:17+00:00

How do I fix createBar() so that a private property bar is created at

  • 0

How do I fix createBar() so that a private property bar is created at the same level as the property foo?

var x = (function() {
  var foo = "a";

  function createBar() {
    this.bar = "b";
  }

  return {
    getFoo: function() {
        return foo;
    },
    getBar: function() {
        return bar;
    }
  }; // end: returned object
}());

This is how the module should work:

x.getFoo(); // returns a
x.getBar(); // returns b
window.bar; // is undefined (but in my example, it is b)

Update:

This is what I am doing now:

var x = (function() {
  var data = {};  //private object holding state variables
  data.foo = "a"; // one state variable

  function createBar() {
    data.bar = "b"; // another dynamically created state variable
  }

  return {
    getFoo: function() {
        return foo;
    },
    getBar: function() {
        return bar;
    }
  }; // end: returned object
}());

but somehow I don’t like it. What would you do, to share state informations between private functions?

  • 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-13T04:09:18+00:00Added an answer on June 13, 2026 at 4:09 am

    Removing the this and declaring bar within the outer function would be the lightest weight solution: however your solution of using the internal data object is the more flexible (though you should be updating your getters to do things like return data.foo;).

    The primary advantage for using the data object would be that if you are building a library of JavaScript code you’d be able to call external functions/mixins within the constructor/private scope of the object you’re creating, while still ending up with a defined interface. As a contrived example if you were doing something like creating an Adapter style of object, you’d be able to call something like linkWithWebService(data) which could populate and add behavior to your privately scoped members upon construction (and those calls would have direct access similar to nested classes in other languages), but all future behavior would still be limited by your defined API.

    Or from personal experience I created an API where the accessors followed the pattern of getter: attributeName() and setter: attributeName(newValue) which I implemented by creating a higher order function to create the getter and an internal data object. Then in the constructor return I did something like attributeName = makeAccessor(attributeName,
    data)
    so the created function became the only exposure of the attribute.

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

Sidebar

Related Questions

Has anyone found a fix for this? I read that it has something to
How can I fix this? Eclipse doesn't recognize this function: listFiles(Filter paramFileFilter) See these
Is it possible to fix the position of a table, such that it scrolls
I have to fix a glitch with this code someone wrote but I'm not
I can't fix this problem on my listview template: i have the error as
To fix a bug in this answer : What's the correct name of the
I'm trying to fix a div at the top of a layout that will
I have to fix a typical memory leak, Problem is like that : typedef
how can I fix this? After a continuous use of the UpdatePanel, it just
In Android, I need same resolution for Default(WVGA800) HVGA QVGA and all emulator that

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.