3

This is regarding javascript closures working. I have a function inside another and I want to access this outside of the outer function. is it possible since it written here that u can achieve closure with this http://www.w3schools.com/js/js_function_closures.asp JavaScript Nested Functions All functions have access to the global scope.

In fact, in JavaScript, all functions have access to the scope "above" them.

JavaScript supports nested functions. Nested functions have access to the scope "above" them.

In this example, the inner function plus() has access to the counter variable in the parent function:

Example

function add() {
    var counter = 0;`enter code here`
    function plus() {counter += 1;}
    plus();    
    return counter; 
}

I am trying to acess plus() from outside

chm-software.com
  • 351
  • 2
  • 10
Rahul Kumar Jain
  • 103
  • 4
  • 13
  • 2
    Some very good answers in this related post [here](https://stackoverflow.com/q/8817872/465053). – RBT Oct 07 '17 at 23:13

4 Answers4

4

Agree with Grim.

But if you wanna access to plus function outside, you can try this way:

function add(){
  var counter = {
      value: 0,
      plus: function(){
         return ++this.value;
      }
  };
  counter.plus();
  return counter; 
}

Hope it helps.

Teddy
  • 787
  • 5
  • 13
2

You cannot. An inner function is only available within the body of the outer function.

Grim...
  • 16,518
  • 7
  • 45
  • 61
  • I got very good explanation of object oriented Javascript http://www.zipcon.net/~swhite/docs/computers/languages/object_oriented_JS/inheritance.html – Rahul Kumar Jain Oct 31 '17 at 17:18
1

I assume your target is to keep value as private property inside add and provide manipulations to it via add.plus() calls:

//define your object with a private "value" and a public modifier "plus"
var add = (function() {
  var counter = 0;
  var that = {
    plus: function() {
      return counter++; //equal to your code
    }
  }
  //your integrated first call
  that.plus();
  return that;
})();

//make a call
add.plus();
chm-software.com
  • 351
  • 2
  • 10
1

DEMO - Working code example.

This may be what you are looking for, especially as related to the tutorial link you provided. It is a step in the right direction.

var plus;
add();
plus();
plus();
plus();
alert(plus());

function add() {
    var counter = 0;
    plus = (function(counter) {
        return function() {counter += 1;return counter;};
    })(counter);
    plus();
}

It is a straight forward example of closure. I made plus a global variable, but alternatively add() could return the function definition of plus. I took the return value away from add() and moved it to plus(), as with this code counter will always equal 1 when add() is finished.

However, and directly from the tutorial you mentioned, the best way to achieve what they are attempting is with this code, ripped directly from their web page.

var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

add();
add();
add(); // the counter is now 3