Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:55:46+00:00 2026-06-06T09:55:46+00:00

I saw this as an example on MDN and didn’t understand why this was

  • 0

I saw this as an example on MDN and didn’t understand why this was a Good Thing (TM).

function makeSizer(size) {  
      return function() {  
        document.body.style.fontSize = size + 'px';  
      };  
    }  

    var size12 = makeSizer(12);  
    var size14 = makeSizer(14);  
    var size16 = makeSizer(16);  

Why is the above better than the below?

function makeSizer(size) {  
        document.body.style.fontSize = size + 'px';  
    }  

    var size12 = makeSizer(12);  
    var size14 = makeSizer(14);  
    var size16 = makeSizer(16);

What does it do differently? Obviously, I’m missing the point…

EDIT: Adding rest of code (full code was in link)

document.getElementById('size-12').onclick = size12;  
document.getElementById('size-14').onclick = size14;  
document.getElementById('size-16').onclick = size16;  

<a href="#" id="size-12">12</a>  
<a href="#" id="size-14">14</a>  
<a href="#" id="size-16">16</a>  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:55:48+00:00Added an answer on June 6, 2026 at 9:55 am

    In this case size is variable that is enclosed. Calling makeSizer(12) returns a function reference that ‘remembers’ 12. This function reference is assigned to a click handler on an element (see the rest of the code in the MDN-example). It says: if you click me, call (execute) size12. On clicking thus the font size of the body becomes ’12px’ because the function reference returned from makeSizer(12) is executed (with the enclosed value of 12).

    If you would’ve used the function you would have resized the font immediately.

    function makeSizer(size) {  
            document.body.style.fontSize = size + 'px';  
    }
    var size12 = makeSizer(12);  
    var size14 = makeSizer(14);  
    var size16 = makeSizer(16); //=> now the font-size of the body is '16px'
    

    Think what would happen in this scenario when you did:

    document.getElementById('size-12').onclick = size12; 
    

    Would anything happen if you clicked #size-12?

    The MDN-page you linked to links to a jsfiddle example. Try it out!

    Good or bad are things I happily leave to the more religious part of the community. I would say this closure is useful, even handy perhaps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in an example i saw this line Thing *pThing = new (getHeap(), getConstraint()) Thing(initval());
I saw this somewhere, For example there is the each function to iterate over
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I saw this in a random doxygen example for C#, and all attempts at
Once, I saw an example like this: var a, x, y; var r =
I was going over C++0x. As i looked at tuple I saw this example.
I saw this example about the fibonacci sequence then tested it in IRB. Was
I saw this example in php manual page http://www.php.net/manual/en/session.examples.php The example will create a
I saw this question Simple example of threading in C++ but my problem is
I saw these tags in Apple's documentation like in this example from the NSFetchResultsController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.