1

So i'm setting up an object with private and public methods. Basically using the following format:

var Utility = function() {
    var prive1, priv2, priv3;
    function privateMethod1() { //do something }

    return {
        publicFunc1: function() { //do something different }
        publicFunc2: function() { //do something else }
    }
}

But i'm worried about some of the situations i'm coming across where publicFunc2 needs to call publicFunc1. For Example the way I would do this atm is:

publicFunc2: function() { Utility.publicFunc1(); //then do something else }

is this OK? It runs, but it seems weird and VS2010 doesn't give me . I believe that if someone was to change the line var Utility = function() { --> to --> var Utility2 = function() {} then essentially everything would be broken from within the object and that seems wrong... but i'm at a loss on what i should actually be changing. Should i be making all methods basically private and then mapping to a public function? EX:

{
    function privateFunc1() {}

    return {
        publicFunc1 : privatefunc1
    }
}

or should i have a completely different approach to accomplish the idea of private and public methods and variables?

3 Answers 3

1
return {
    publicFunc1: function() {  },
    publicFunc2: function() { this.publicFunc1()  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What if someone will replace that publicFunc1 in the object? I mean this approach has side effects.
I do not understand the purpose of your changes. Someone still can change publicFunc1 field value. So this.publicFunc1() may not call the function that author meant. It may or may not be acceptable in principle - depends on requirements.
oh, sorry, I've not understand you at first. really, you're right
0

If you want to call some function - give it a name and call it by that name:

var Utility = function() {
    var prive1, priv2, priv3;
    function privateMethod1() { //do something }

    function Func1() { //do something different }
    function Func2() { Func1(); //do something else }

    return {
        publicFunc1: Func1,
        publicFunc2: Func2
    };
}

Call of local function by name is always faster than any other method of call in JS.

1 Comment

Ok, this seems like the best answer thus far, I just felt this approach was a bit wordy and would make my javascript to long... but i suppose it really isn't that bad. Thank you.
0

In cases like this remember the YAGNI (You ain't going to need it) concept. Sure, think about the best way to organise things but code for now intially and then refactor it later if needed.

Do the functions need to public or are you second guessing the functionality that may be needed later? Make them private, have a single public method for now. Refactor later if needed. Keep it simple.

1 Comment

Well i'm basically already at the re-factoring stage, I have it working... but it's not in a maintainable state... and i'm trying to mitigate the number of times this core logic needs to be re-factored, but i def appreciate the advice.. i havn't heard YAGNI before, i like it :P

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.