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

  • Home
  • SEARCH
  • 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 6006729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:34:54+00:00 2026-05-23T01:34:54+00:00

I have been mauling over this for while now, and wonder if anyone could

  • 0

I have been mauling over this for while now, and wonder if anyone could point me in the right direction.

I have the following object definition:

var foo=function(){};
foo.prototype={
  foo1:function(){},
  baz:{
    baz1:function(){}
  },
  bar:function(){
    function privateFunc1(){}
    function privateFunc2(){}
    return {
      bar1:function(){
        //do something using privateFunc1
        //return the result
      }
    };
  }
};

var foobar = new foo();

Now, to access the baz1 sub-method of the baz method, I can do foobar.baz.baz1()
But to access the bar1 sub-method of the bar method, I have to do foobar.bar().bar1() (Notice the extra parens after bar)

Is there a way of defining the foo object so that I can call bar1 using foobar.bar.bar1() (Notice, no extra parens), But still keep the use of private functions privateFunc1 and privateFunc2 within the bar method.

Also, please note that I cannot make bar a self-executing function because it depends on mutable properties of the foo object, which may change after the function has self-executed.

Hope the question was clear enough… thanks in advance.

  • 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-23T01:34:54+00:00Added an answer on May 23, 2026 at 1:34 am

    This might be going in the direction you want.
    http://jsfiddle.net/Una9k/1/

    var Foo = function() {
        this.mutableProperty = "mutable";
        this.bar = new Foo.prototype.Bar(this);
    };
    Foo.prototype = {
        constructor: Foo,
        Bar:function(context){
            var ctx = context;
            function privateFunc(){
                alert(ctx.mutableProperty);
            }
            return {
                bar1: function () {
                    privateFunc();    
                }
            };
        }
    };
    
    var foobar = new Foo();
    foobar.bar.bar1(); // alerts 'mutable'
    
    foobar.mutableProperty = "changed";
    foobar.bar.bar1(); // alerts 'changed'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been following this fabulous tutorial . Being new to Javascript and functional programming,
I have been working for a while on a project with the following components:
Have been working on this question for a couple hours and have come close
I have been working with SQL Server as a Developer a while. One thing
I am sure this has been asked already, but I have been trying all
I feel like this question ought to have been covered, but I can't seem
Have been learning ASP.NET (using C#) over the past few days. I have made
Have been trying to encrypt an xml file to a string so that I
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
Have been reading about async and tasks and been attempting to convert the CopyFileEx

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.