3

I know that you can write following

var obj = {
    test: 'something'
}

But in this code, the inner function does not refer to a variable, but to a function.

Is there any other way to write / call the inner function?

function outer(){
    
    var a = "Outerfunction";
    console.log(a)
            
    innerFct: function InnerFct()    {                                              
        var c = "Inner";
        console.log(c)
    } innerFct();
}
window.outer();

3 Answers 3

3

There are a couple of different things going on here.

In this code:

var obj = {
    test: 'something'
}

you are using "literal object notation" to create -- well, an object with one property test and that property has a value of something

In your second case, you are creating a code block (yes, it is fun that both objects and code blocks use the same syntax {...} to define them.

Inside of a code block, the innerFct: becomes a label. Labels are used with some control flow statements to jump around. Forget about them, you really are better off not using them.

function outer(){
    var a = "Outerfunction";
    console.log(a)

    function innerFct()    {                                              
        var c = "Inner";
        console.log(c)
    }
    innerFct();
}
outer();

or even

function outer(){
    var a = "Outerfunction";
    console.log(a)

    var innerFct = function ()    {                                              
        var c = "Inner";
        console.log(c)
    }
    innerFct();
}
outer();
Sign up to request clarification or add additional context in comments.

5 Comments

thank you, that was exactly what I was looking for. Yes I know, that the first one was a literal object, but I wonder why the function works with ":"
Your original code throws an error: ReferenceError: innerFct is not defined for me. What browser are you using where it works?
Hi @JeremyJStarcher When I write below code: var myObject{ function talk(){ console.log('Hello world'); } } myObject.talk() but it throw me an error: Uncaught SyntaxError: Unexpected identifier
Correct, it is a syntax error. Google 'Revealing Module Pattern' for lots of discussion on what you are trying to do.
1

You are confusing functions with objects.

When using an object, the colon is used to show key-value pairs.

var object = {
  innerFct: function(){
    console.log('rawr');
  },
  someVariable: 7
}

object.innerFct(); //logs rawr
object.someVariable = 4; //just changed from 7 to 4

Using a colon how you have it in your example is incorrect syntax. Also when you are creating a function within an object like that, you don't name the function again because you are already assigning it to a name on the object.

Then you can edit the function anytime by doing something like this:

object.innerFct = function(){
  //new code
}

Doing object.innerFct() will call the function.

Comments

0

Other answers have sufficiently covered the object syntax and calling the function in scope. As I mentioned in the comment, you can just do this:

function outer(){
    (function () {
        var c = "inner";
        console.log(c)
    })();
}
window.outer();

And it logs inner just fine.

Edit: Private/hidden variables like innerFct in the original code sample can be captured in closures as well.

outer = function() {
    var innerFct = function () { console.log("inner"); }
    // innerFct is captured in the closure of the following functions
    // so it is defined within the scope of those functions, when they are called
    // even though it isn't defined before or after they complete
    window.wrapper = function() { innerFct(); }
    return function() { innerFct(); }
}
outer();

// each of these next three lines logs "inner"
window.wrapper();   // assigned to global variable
outer()();          // calling the returned function
var innerFctBackFromTheDead = outer(); // saving the returned function
innerFctBackFromTheDead();

There is also the object constructor/prototype syntax.

function Outer() {
    this.inner = function() {
        this.c = "inner";
        console.log(this.c);
    }
}

var out = new Outer();
out.c;       // undefined
out.inner(); // logs "inner"
out.c;       // "inner"

More information on the new keyword and prototypes: http://pivotallabs.com/javascript-constructors-prototypes-and-the-new-keyword/

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.