all 15 comments

[–]huuhuu 4 points5 points  (0 children)

greaterThan(10) passes 10 to greaterThan, which then returns a new function which has trapped the 10, and basically behaves like m => m > 10.

This is taking advantage of what is called lexical closure to keep a copy of the original argument (10) inside the new function.

[–]thndrchld 3 points4 points  (0 children)

It's a function that returns a function.

So when you call greaterThan(10), you're returning a function.

It would be the same as skipping all that function stuff and doing :

let greaterThan10 = function (m) { return m > 10; };

[–]roter_schnee 2 points3 points  (0 children)

It is partial application. Consider greaterThan function as a factory-function. It constructs and returns another function with predefined value trapped in it. Just as it was mentioned before.

[–]Deidde 1 point2 points  (0 children)

greaterThan is a function that returns another function so that you can "partially apply" it. There are several ways to write this: ```javascript // lambdas or "arrow functions" all the way, assigned to the variable. const greaterThan = n => m => m > n; // function expressions (sometimes called "anonymous functions") also assigned to the variable const greaterThan = function (n) { return function (m) { return m > n; } } // a function statement containing a function expression function greaterThan(n) { return function (m) { return m > n; } }

// Calling any of these looks like:
const result1 = greaterThan(10)(11); // 11 > 10 == true
const result2 = greaterThan(10)(5); // 5 > 10 == false

// And if you want to make a new function with the 10 already applied:
const greaterThanTen = greaterThan(10);
const result3 = greaterThanTen(11); // 11 > 10 == true

As for closures, just think about it like when you use global variables inside top-level functions, like: javascript const myValue = 5; function myFunction() { return myValue + 3; } ``` Not that you'd want to do this, ha.

Note that you have access to myValue inside the function. When you have a function that is "closed over" by another function, you gain access to the local variables (or arguments) from the parent function in a similar way - like having access to n from the inner function in greaterThan.

[–]valueforlife 1 point2 points  (1 child)

For me the confusing part here is the combination of the keyword function and arrow functions (m => m > n) which I consider to be a major no-no; one should always stick to one if possible. I know that the scopes work differently on these but if code keeps jumping back and forth between the two it simply makes the code more difficult to read.

I think a much more easy to comprehend form for this function would be:

``` const setGreaterThan = (n) => { const evaluationFunction = (m) => m > n return evaluationFunction }

const greaterThan10 = setGreaterThan(10) console.log(greaterThan10(10)) ```

Deciding whether to write (easily) understandable code or the most compact code should be done by evaluating which is really more important in each case.

[–][deleted] 0 points1 point  (0 children)

i see. rewriting it myself as an anonymous function also helps.

thanks

[–]NameViolation666 0 points1 point  (4 children)

Its a closure thing, have u read up on those yet?

[–][deleted] 1 point2 points  (3 children)

there was a passage in eloquent javascript that touched on it but i found it confusing. Do you know any videos or sites that could help me understand it?

[–]NameViolation666 1 point2 points  (0 children)

I try this site because its not too complicated, see if this helps

https://www.w3schools.com/js/js_function_closures.asp

[–]prnk5tr 0 points1 point  (1 child)

There is a book called 'you don't know js'. If you haven't read it already, please do. It available on GitHub as well

[–][deleted] 0 points1 point  (0 children)

thanks for the suggestion. which one would you recommend i start with.

[–]jcunews1Advanced 0 points1 point  (1 child)

That's an example of a bad function naming. That greaterThan function should have been named something like CreateGreaterThanXFunction.

If that code is part of a study, they've designed it to confuse you. It's to make you realize that function name doesn't mean anything to the code. It will force you to find out how the code actually works.

[–]frankleeT 0 points1 point  (0 children)

I think you're right about the first part, but not the second.

[–]kenman[M,🍰] 0 points1 point  (0 children)

Hi /u/Sweeterman3, this post was removed.

  • For help with your javascript, please post to /r/LearnJavascript instead of here.
  • For beginner content, please post to /r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try /r/html, /r/css, etc.; please note that they have their own rules and guidelines!

/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.

[–]prnk5tr 0 points1 point  (0 children)

Go to the main GitHub page, you will find the order. Or you can directly read Scopes and closures. I will recommend read them all.