1

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.

3 Answers 3

2

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'
Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps this might work...

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

   foobar.bar.bar1 = foobar.bar();

   return foobar;
};

var foobar = createFoobar();

Using the above you could also house your functions on the createFooBar function. So after the above...

createFooBar.foo1 = function(){} //and so on...

then in the createFooBar function you might refer to foo1 like so...

//code in the beginning...
var foobar = {
    foo1: createFooBar.foo1,
//and so on...

Additionally, you could ALSO just create a create function on your current foo function...

foo.create = function() {
    var fooBar = new Foo();
    fooBar.bar.bar1 = fooBar.bar();
    return fooBar;
}

var fooBar = foo.create();

Comments

1

If it is possible to call foobar.bar() immediately:

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


   return foobar;
};
// now you have foobar.bar.bar1()

1 Comment

can't self-execute the function because it depends on mutable properties of the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.