0

I'm doing the tutorials for functional programming on the nodeschool-homepage. I'm new to JS (came from Java) so I don't get some aspects of JS, for example:

function say(word) {
   return function(anotherWord) {
        console.log(anotherWord);
    }
}

If I call:

say("hi"); // it returns nothing

say("hi", "hi"); // it returns nothing

var said = say("hi"); // asignment

said("hi"); // returns hi -- but why?

said(); // returns undefined;

Can someone explain to me how the "hi" in the outer function is passed in the inner function?

0

3 Answers 3

2
said("hi"); // returns hi -- but why?

Because the inner function is defined as

function(anotherWord) {
    console.log(anotherWord);
}

This means it logs the first argument it gets passed. You are passing 'hi', so it logs 'hi'.

how t.. f... the "hi" in the outer function is passed in the inner function?

It isn't. The inner function only accesses its own argument. It doesn't matter what you pass as argument to the first function. say()('hi'), say('foo')('hi') are all equivalent. What matters is the argument passed to the second function.

If instead you defined the function as

function say(word) {
   return function(anotherWord) {
        console.log(word, anotherWord);
    }
}

Now the inner function also accesses the first argument of the outer function, so you would get different results.

Why does this work? Because all functions in JS are closures and so they have access to variable bindings in higher scopes.

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

Comments

0

The function say takes one parameter word which is not used anywhere, so nothing is passed from the "outer" function to the "inner" function.

function say(word) {
    return function(anotherWord) {
        console.log(anotherWord);
    };
}

What happens is, that say() returns a function. This returned function takes one parameter (anotherWord), which is output to the console when called.

Now to your code samples:

say("hi"); // this returns a function, but you don't keep it anywhere

say("hi", "hi"); // still returns a function, still no variable to keep it

var said = say("hi"); // finally the returned function is saved, the param is not used

said("hi"); // this prints "hi", because you pass it 'hi'

said("foobar"); // this prints "foobar", because you pass it 'foobar'

said(); // This prints undefined, as you did not pass any parameter

Comments

0

The function say returns a function that takes a parameter and logs it. The parameter passed to the say function (the parameter word) is discarded.

The instance of your code that appears to work is the following:

var said = say("hi");
said("hi");

When you call the the say function in the first line here, the word "hi" that you pass in does nothing. That function returns another, anonymous function which is stored in the said variable.

When you call the function in the said variable and pass in "hi", it operates as expected. It would work exactly the same way if the first line was

var said = say("elephant");

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.