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

1
  • 2
    Some very good answers in this related post here. Commented Oct 7, 2017 at 23:13

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

in OP value of counter was changed, in your - you only return value, not change value
this in your case refers to the function counter.plus so this.value will be undefined as ther is no counter.plus.value
this refer to counter, not counter.plus.
2

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

1 Comment

I got very good explanation of object oriented Javascript zipcon.net/~swhite/docs/computers/languages/object_oriented_JS/…
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();

Comments

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

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.