Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active November 10, 2015 07:54
Show Gist options
  • Save zulhfreelancer/1f39787671981611640b to your computer and use it in GitHub Desktop.
Save zulhfreelancer/1f39787671981611640b to your computer and use it in GitHub Desktop.
How to call a function inside a function?

You should put your function inside a variable so that you can access it like object.

var toggleVis = (function() {
    return {
        showSomething : function () {
          console.log("showSomething");
        },

        hideSomething : function () {
          console.log("hideSomething");
        }
    };

}());

toggleVis.showSomething();

Will print:

showSomething

To call a function inside that function, make it like this:

var toggleVis = (function() {
    return {
        showSomething : function () {
          console.log("showSomething");
          this.hideSomething();
        },

        hideSomething : function () {
          console.log("hideSomething");
        }
    };

}());

toggleVis.showSomething();

That will print: showSomething

hideSomething

In this case, this is referred to the toggleVis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment