1

I'm understanding "Scope" I found this code, but Im wondering how can I execute the "inner" function, I tried like this:

outer().inner();

but doesn't works

        /* global scope */
        var local = true;
        var global = true;
        function outer() {
            /* local scope */
            var local = true;
            var global = false;

            /* nearest scope = outer */
            local = !global;
            console.log("Local: "+local);
            console.log("Global: "+Global);

            function inner() {
                /* nearest scope = outer */
                local = false;
                global = false;

                /* nearest scope = undefined */
                /* defaults to defining a global */
                public = global;
            }
        }
1
  • you can modify the outer function to add a statement to return the inner function "return inner;" and then you could call the inner function as var inner = outer(); inner(); Commented May 29, 2015 at 19:16

5 Answers 5

2

You could change outer to be an object instead of a function.

/* global scope */
var local = true;
var global = true;
var outer = {
    /* local scope */
    local : true,
    global : false,

    /* nearest scope = outer */
    local : !global,

    showLogs: function(){
        console.log("Local: "+local);
        console.log("Global: "+Global);
    },

    inner: function(){
        local = false;
        global = false;
        public = global;
    }
}

Notice that I'm declaring outer as an object instead of a function with var outer={} That way you can call the functions with outer.inner(); and outer.showLogs();

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

2 Comments

Great Thanks! On comment I think I must put commas instead of semicolons :D !
Whoops! You're absolutely right. I just edited it to show commas.
1

In order to access inner(), outer() would need to return inner().

Example:

function outer() {
  ...

  return {
     inner: function inner() {
       ...
     }
  };
}

outer().inner();

Another kind of interesting thing that's possible:

function outer() {
  return function inner() {}
}

outer()();

2 Comments

Oh Very interesting! The Second one is like an "Anonymous function" inside a function right?
Yeah, you don't really need to name that inner function. I just included that for illustration's sake. return function() { } would have worked the same way.
1

You cannot do that. You can make the inner function a method if you want and call that... like so:

http://output.jsbin.com/wuseqonuxa/2/edit?js,console (You will get the console logging "Im running").

/* global scope */
var local = true;
var global = true;

function outer() {
    /* local scope */
    var local = true;
    var global = false;
    
    /* nearest scope = outer */
    local = !global;
    console.log("Local: "+local);
    console.log("Global: "+global);
    
    this.inner = function () {
        /* nearest scope = outer */
        local = false;
        global = false;
        
        /* nearest scope = undefined */
        /* defaults to defining a global */
        public = global;
        console.log('Im running');
    }
}

var outer = new outer();
outer.inner();

1 Comment

Is like if inner is a method now? I mean outer is now an Object and inner is a method ?
1

While inside outer, you can simply call inner(). It is defined inside outer and can't be called outside of outer as is. However, you can set outer to return 'inner'. @jthomas shows a couple examples of this below.

Comments

0

The inner function can only be accessed by code within outer. Code outside of outer cannot access inner.

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.