JavaScript javascript private inner functions in module with access to “this” scope [duplicate] javascript private inner class

This question already has an answer here:
Answer:1

Declare myvar using the var keyword, making it private, then access it without the this.:

function MyObj(){

        var myvar = 123;

        this.publicfunc = function() {
            someOtherFunc();
        };

        var someOtherFunc = function(){

            alert(myvar);

        };            
}


var o = new MyObj();
o.publicfunc();

If you need public access to myvar then create a public getter/setter.

jsFiddle Demo

Answer:2

I think what you're looking for is a way to encapsulate myvar for changes. When some of the other answers run, myvar usually will stay as 123 because the initially returned object from mymodule holds on to that initial value.

Return a function that gets the value of myvar even after it's been modified, and I think that helps your problem.

Here's the code that works for me:

var mymodule = (function(){
    var myvar = 123,
        publicfunc = function() { myvar = 456 },
        getMyVar = function() { return myvar; };

    return {
        someOtherFunc : publicfunc,
        myPublicVar : getMyVar
     };
}());

mymodule.someOtherFunc();
alert(mymodule.myPublicVar());​  //gives you 456

JSFiddle here.

I hope this helps.

Answer:3

Why not build it a little more deliberately?

// this one returns an object used like:
// myModule.myInt; /* 456 */
// myModule.myFunc(); /* 124 */
var myModule = (function () {
    var secretData = 123,
        publicData = 456,

        publicFunc = function () { return privateFunc(secretData); },
        privateFunc = function (num) { return num + 1; },

        public_interface = {
            myInt  : publicData,
            myFunc : publicFunc
        };

    return public_interface;

}());

I went through the trouble of explicitly naming the returned, public object, but it's now very clear what is and isn't public, and yet, each one of those things will have access to the variable versions of one another, with the one exception being that if you change myModule.myInt or publicData, they will no longer be equal.


To demonstrate what I mean in the comments below, creating multiple instances with their own private data/methods, I just add in one more layer of function-scope:

var myModule = (function () {

    var static_int = 789,

        makeInstance = function (/* any constructor values */) {
            var secretData = 123,
                publicData = 456,

                publicFunc = function () { return privateFunc(secretData); },
                privateFunc = function (num) {
                    console.log(static_int);
                    return num + 1;
                },

                public_interface = {
                    myInt  : publicData,
                    myFunc : publicFunc
                };

            return public_interface;
        };

    return makeInstance;
}());

You now use it like:

var newModule = myModule(/* instance parameters */);
newModule.myFunc();

...or

var num = myModule(/* instance parameters */).myFunc();

If you wanted to save memory, you could have static helper functions inside of the static-layer:

var myModule = (function () {

    var static_int = 789,

        static_addOne = function (num) { return num + 1; },
        static_divideBy = function (dividend, divisor) { return dividend/divisor; },

        makeInstance = function (/* any constructor values */) {
            var secretData = 123,
                publicData = 456,

                publicFunc = function () { return privateFunc(secretData); },
                privateFunc = function (num) {
                    console.log(static_int);
                    return num + 1;
                },

                public_interface = {
                    myInt  : publicData,
                    myFunc : publicFunc
                };

            return public_interface;
        };

    return makeInstance;
}());

And now you have "private" functions which are only written one time (ie: you save memory), but any instance can use those functions.

Here's the catch:
Because of how scope and closure work, the static functions have NO access to values inside of the instance (functions inside have access to the static functions, not the other way around).

So, any static helper functions MUST have the values passed to them as arguments, and if you're modifying a number or a string, you MUST return the value out of that function.

// inside of a private method, in any instance
var privateString = "Bob",

    privateFunc = function () {
        var string = static_helper(privateString);
        privateString = string;
        //...
    };
Answer:4

You don't return anything from MyObj.

return this;

should fix it.

Answer:5

i can find text of an element by using this code.but i want to find a single word any where in my page if mouse hovers on that word for certain time. $(document).bind("mouseover", function (e) { ...

i can find text of an element by using this code.but i want to find a single word any where in my page if mouse hovers on that word for certain time. $(document).bind("mouseover", function (e) { ...

  1. find word under cursor vim
  2. word find under the sea
  3. vscode find word under cursor
  4. emacs find word under cursor
  5. intellij find word under cursor
  6. vi find word under cursor
  7. eclipse find word under cursor
  8. visual studio find word under cursor
  9. vim find next word under cursor
  10. search for word under cursor vim
  11. search word under cursor vim
  12. get word under cursor vim
  13. word under cursor vim

I've attached an autocomplete function to a text input field in an html form. I can handle when the user selects something in the suggest box and it all works fine. I use "suggest" as source. My ...

I've attached an autocomplete function to a text input field in an html form. I can handle when the user selects something in the suggest box and it all works fine. I use "suggest" as source. My ...

Jquery Ajax call only fires once after that if i click on the button it does return the last returned reponse but doesn't go the ajax function in c# Here is my aspx code <asp:ImageButton ID="...

Jquery Ajax call only fires once after that if i click on the button it does return the last returned reponse but doesn't go the ajax function in c# Here is my aspx code <asp:ImageButton ID="...

  1. jquery ajax call example
  2. jquery ajax call to mvc controller
  3. jquery ajax call post
  4. jquery ajax call get
  5. jquery ajax call on button click
  6. jquery ajax call data
  7. jquery ajax call post example
  8. jquery ajax call synchronous
  9. jquery ajax call return value
  10. jquery ajax call in mvc
  11. jquery ajax call error
  12. jquery ajax callback
  13. jquery ajax call success
  14. jquery ajax call async
  15. jquery ajax call on click
  16. jquery ajax call url
  17. jquery ajax call rest api
  18. jquery ajax call success function
  19. jquery ajax call syntax
  20. jquery ajax call not firing

to keep things simple, I have jquery autocomplete working, what is the best way to approach, joining/displaying a multiple rows from the array in the same input field. My php looks like this $...

to keep things simple, I have jquery autocomplete working, what is the best way to approach, joining/displaying a multiple rows from the array in the same input field. My php looks like this $...

  1. jquery autocomplete best
  2. jquery autocomplete best practices
  3. jquery autocomplete top position
  4. jquery autocomplete top 10
  5. jquery autocomplete top
  6. best jquery autocomplete plugin
  7. best jquery autocomplete 2018