Better understanding higher order functions - helpful resources

That line invokes the function that returns another function. There won’t be any return except that other function.

We would normally assign this to another variable and invoke it at a later time; but, what your instructor has done is immediately invoke the returned function. Examine the code closely…

const showName = function() {
    const name = “FP JavaScript”
    return function() {
        console.log(name)  // note the internal logging
    }
}()    // invoke the returned function
1 Like

Hello Roy. Can you give me a hand here by any chance? At the very last line, how does get understand that it’s called on the people array?

It ‘knows’ because that is the argument passed into the function that is returned from the map() factory function.

My head will explode now. Thanks for your time but I don’t think I got it.

Functions may take an argument that is itself a function, and/or may return a function object to the caller.

The first function above, get() takes a value, specifically a property name from an object literal, or an index for an array object.

function get(prop) {
  return function(obj) {
    return obj[prop]
  }
}

The function that is returned looks like,

function(obj) {
  return obj[prop]
}

where prop in this case is the value that was passed to the get() function.

Let’s consider,

get_prop = get('property')

Now get_prop is itself a function with a scoped internal value, such that, prop === 'property'. If we call that function with the argument, {property: 'value'} (an object) it returns the value’.

console.log(get_prop({property: 'value'}))
 // should print, `value`

In like fashion, the map() function takes a function as its argument and returns a function with a scoped internal value, the function we passed in. That function is the one returned by get().

The returned function takes an array as an argument. Together with the internal function, these are both passed as arguments of .call() method.

array_map = map(get_prop('property'))

Now when we call that function, we give the name of the array we wish to map the return from get_prop().

objs = [{property: 'some value' }, {property: 'some other value' }]

console.log(array_map(objs))
// should print [ 'some value', 'some other value' ]

In the above example we assigned each returned function to it own variable. The example in your post doesn’t, but only passes the actual functions anonymously.

2 Likes

Thank you so much! Don’t think it could be any more clarified than that. I think I’m really getting an idea of how the arguments can be “chained” upon calling, like map(get('email'))(people), and mechanics behind that. I’ve saved your kind explanation to get back to it in a while for a hopefully even better (or at least more fluent) understanding. Thank you.

2 Likes

You’re welcome. Wasn’t sure it was clear enough given so many moving parts. Glad to hear you making some headway.

1 Like

This lesson is seriously bad.

I don’t know whether it’s the instructions, the examples used or just the general lack of information but I have literally no idea what’s going on and I’ve struggled with nothing on this course up to this point.

Definitely needs a rewrite because it might as well be in Chinese.

1 Like

I have to agree with all the other students here. This lesson is extremely complicated and don’t believe the instructions are well written.

It seems there is a lot of context missing that would be helpful. For example, to even begin understanding HOF I think it is imperative to incorporate what exactly an ‘object’ is. Which I believe has not been taught yet…

Potentially, I think simplifying the use case would be helpful. The article below is what allowed me to slightly know what’s going on.

Going to add to other comments here. I’ve been doing well with the lessons, but this one just completely stumped me. Very difficult to understand the instructions, what the purpose is, how to progress. The whole lesson ought to be re-written.

If there wasn’t for the Forum, I wouldn’t have gotten to the end of the lesson of humble three steps. Feeling clueless gradually mesmerised me in realisation I wasn’t alone.
Reading linked articles below the third task, about HOF, and related info users posted in a collective effort to overcome this hurdle, started clearing my mind.

Thinking of those problems were breaking my streaks, hopefully not for long and I haven’t given up learning to code. Is it for me keeps hanging over.

It dawned on me the next day, connecting apparently insignificant event of having received an Email from Codecademy; the quest earned me a badge.

izdvojeni citat

https://discuss.codecademy.com/badges/130/bookworm-bronze?username=nenad1

izdvojeni citat

Witnessing mtf’s (Codecademy moderator’s) eloquence in slicing inferred lesson to code bids lavishly commenting between the lines, made me think perhaps this lesson was purposely crippled. I have a shelf filled with books from the previous decades, where this dimension is absent completely. Feels so integrated at Codecademy.

Our grasping for clues is as important to the lesson’s completion, if not more. Answers are around us and this way Codecademy is growing us a habit of seeking and finding help. Quitting button remains available, but re-routing is much more productive then having erroneous code replaced with a correct one.

Much obliged Roy!

To my understanding, perhaps the most valuable trait of Codecademy is the attitude of hinting replies, encouraging us to pursue, instead of copy/paste remedy on problems which brought students to a halt.

1 Like

Best explanation I found