Skip to content

Conversation

mao-sz
Copy link
Contributor

@mao-sz mao-sz commented Sep 4, 2025

Because

Due to some inconsistencies with code style and usage of comments, some learners miss some things or get confused in the lesson flow when some parts are in normal text and some parts are in longer comments.

There has also been common confusion regarding the point of IIFEs or what IIFEs actually are (perhaps due to focusing immediately on a practical use of IIFEs without giving a more plain example of what an IIFE really just is - people sometimes overthink it's some special specific construct).

This PR

  • Tweaks code blocks
    • Reorganises comments so that key lesson text is now normal text, with those code blocks split for easier following
    • Uses more consistent formatting across lesson (should better match other lessons too)
  • Rephrases ESM note box to more clearly link it with the lesson's section topic
  • Adds some clarifying content on definition and purpose of IIFE to cover some gaps in the current explanations

Issue

Closes #29212

Pull Request Requirements

  • I have thoroughly read and understand The Odin Project curriculum contributing guide
  • The title of this PR follows the location of change: brief description of change format, e.g. Intro to HTML and CSS lesson: Fix link text
  • The Because section summarizes the reason for this PR
  • The This PR section has a bullet point list describing the changes in this PR
  • If this PR addresses an open issue, it is linked in the Issue section
  • If any lesson files are included in this PR, they have been previewed with the Markdown preview tool to ensure it is formatted correctly
  • If any lesson files are included in this PR, they follow the Layout Style Guide

Some code blocks split to be easier to follow than trying to follow
large comments
Style is more consistent with the rest of the curriculum as well.
Heading added as now required by style guide.
Rephrased to link general concept of modules with the current lesson
section.
Addresses common confusion with "why not just call the factory function
once and omit the IIFE?"
Common confusion that IIFEs are a special construct and not just a func
exp called immediately (due to immediate focus on the module pattern).

The module pattern doesn't have to be used with factory functions only,
just it's one of the more useful use cases.
@github-actions github-actions bot added the Content: JavaScript Involves the JavaScript course label Sep 4, 2025
Comment on lines +62 to 79
function makeAdding(firstNumber) {
// "first" is scoped within the makeAdding function
const first = firstNumber;
return function resulting (secondNumber) {

return function resulting(secondNumber) {
// "second" is scoped within the resulting function
const second = secondNumber;
return first + second;
}
}
// but we've not seen an example of a "function"
// being returned, thus far - how do we use it?
```

But we've not seen an example of a "function" being returned thus far - how do we use it?

```javascript
const add5 = makeAdding(5);
console.log(add5(2)) // logs 7
console.log(add5(2)); // logs 7
```
Copy link
Member

@ManonLef ManonLef Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidentally came by your PR. It could be me but I'm very not charmed by the function naming here. Since this lesson is being worked on, could we consider something else? For the returned function I think add or addSecondNumber would work better and is about as descriptive as it gets. resulting does not sound like/is a conventional js name for a function.

For the enclosing makeAdding function I'm not sure yet. Some ideas: makeClosureAdder, makeAddFunction although I'm not a fan of using the function word inside the name but it's very descriptive, createAdder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glossed over this but I agree re: the function names. I also think there's no need for the extra variables - the params themselves are variables scoped to their own functions.

How does this look:

// firstNumber is scoped to the makeAdder function
function makeAdder(firstNumber) {

  // secondNumber is scoped to addSecondNumber
  // This function doesn't need to be named, we're just
  // making it easier to refer to this function later
  return function addSecondNumber(secondNumber) {
    return firstNumber + secondNumber;
  }
}

Alternatively, after a more thorough read of this section on closures, I think the section could be simplified and perhaps given a second example at the end that's more practical. This adding function is more appropriate for a simple rundown of the mechanics.

I think part of the simplification could be making clear this is no different from any other use of functions. If you want to create a string based off some args, you write a function that returns a string. Want an array based off args? Same thing - write a function that returns an array. These are all reusable. Want a function based off some args? Write a function that returns a function.

I've found this perspective pretty successful in the server for demistifying closures. I feel this may be a little out of this PR's scope, so if you agree with the above, I think it'll be best to handle that in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'll have a little deeper think about this section and repurpose this PR as more content clarification with a side of tidyup, rather than the reverse.

@rlmoser99 rlmoser99 requested review from a team and JustWaveThings and removed request for a team September 8, 2025 00:56
@mao-sz mao-sz marked this pull request as draft September 10, 2025 14:47
Copy link
Contributor

@JustWaveThings JustWaveThings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'll have a little deeper think about this section and repurpose this PR as more content clarification with a side of tidyup, rather than the reverse.

That sounds like a solid plan. I think your example code above is an improvement as well. Thanks for doing this Mao.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content: JavaScript Involves the JavaScript course
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Factory Functions: Suggested style tidy-up and phrasing tweaks
4 participants