1

I want to rewrite a file to incorporate public and private vars. I am trying to do something like so:

var PrivateFunc = (function() {

  //Private Functions
  var _iAmPrivate = function(param, args){
    //code
  }

  var _soAmI = function(param){
    //code
  }

  return PrivateFunc;
})();

var PubObj = {
  someVar: 1,
  getPrivFunc: PrivateFunc,

  publicFunc: function(param){
    //This doesnt work
    this.getPrivFunc._iAmPrivate(someVar, param);
    //This doesn't either
    getPrivFunc._soAmI(param);
  }

};

I am a "getPrivFunc is undefined" error. I thought that moving getPrivFunc outside of PubObj could work, but i ran into the same issue. Calling PrivateFunc give errors as well. I feel like it is a silly error, I am just not seeing it.

Maybe there is a better way to do this?

1
  • "I am a "getPrivFunc is undefined" error." That's because PrivateFunc is undefined. What you actually do is assigning the variable to itself (return PrivateFunc;). It's the same as var foo = foo;. But since foo (PrivateFunc) was never assigned a different value, its value will be undefined. Commented Jun 21, 2013 at 19:23

2 Answers 2

1

Anything declared with a var is going to be inaccessible outside the function it's declared in. You could do something like the following, instead. The important thing to keep in mind is that where your vars occur defines the scope of their declaration.

var PubObj;
(function() {
  PubObj = {
    someVar: 1,

    publicFunc: function(param) {
      _iAmPrivate(this.someVar, param);
      _soAmI(param);
    }
  };

  //Inner Functions
  var _iAmPrivate = function(param, args) {
    //code
  };

  var _soAmI = function(param) {
    //code
  };
})();
Sign up to request clarification or add additional context in comments.

3 Comments

That makes sense. I could see how that could work. I was guessing that keeping everything wrapped in PubObj would work too? This is a rewrite of an existing file, and PubObj is called many times throughout the program. Or would it be something like var p = PubObj; p.publicFunc(param);?
Just updated the sample; does that fit with what you're asking? The way it's defined now, PubObj is accessible both from within the "private" functions and in the global scope of the file.
Exactly what I wanted. Thanks
1

Your PrivateFunc wasn't returning anything, because you're using OOP concepts without proper implementation. If we create a function that sets a property on this for each of your private functions, you can create an instance of it with the new keyword.

var PrivateFunc = new function() {
  this._iAmPrivate = function(param, args) {
    console.log('PrivateFunc._iAmPrivate called with', param, args);
  }

  this._soAmI = function(param) {
    console.log('PrivateFunc._soAmI called with', param);
  }
}();

We can then access PrivateFunc from inside of your PubObj.

var PubObj = {
  someVar: 1,
  getPrivFunc: PrivateFunc,

  publicFunc: function(param) {
    this.getPrivFunc._iAmPrivate(this.someVar, param);
    this.getPrivFunc._soAmI(param);
  }
};

When we call PubObj.publicFunc('test'), we get:

PubObj.publicFunc('test')
> PrivateFunc._iAmPrivate called with 1 test
> PrivateFunc._soAmI called with test

Comments

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.