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

  • Home
  • SEARCH
  • 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 8096889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:35:06+00:00 2026-06-05T21:35:06+00:00

While developer a client side application I ran into an error that I believe

  • 0

While developer a client side application I ran into an error that I believe is related to my incomplete understanding of use of closures. I have trimmed down my code to the following :


var fn1 = function(arr){ 
  return function(val){ 
    var idx;
    var x = 20; 
    for (idx in arr) {
      arr[idx]();
    } 
  } 
}

var fn2 = fn1([function(){
  x+= 20; 
  alert(x);
}])

Now upon executing :

fn2()

I receive an error :

ReferenceError: reference to undefined property "x"

I would like to know why this error is arising and why can’t fn2 access the variables defined in the local scope of closure function returned by fn1 ?

  • 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-05T21:35:08+00:00Added an answer on June 5, 2026 at 9:35 pm

    In short: closures have access to parent scopes in relation to where they were declared, not where they are used.

    The function returned by fn1 has access to the scope of its parent function (fn1 itself), i.e., it has access to arr. But the functions inside your array do not have access to variables defined within fn1 or within the function returned from it, because those functions (inside array) were declared in a different scope.

    Regarding the actual problem you’re trying to solve with that code, I’m not sure if I understood it correctly. Looks like you’re trying to apply some functions to a certain number. I don’t understand why that number (20 in your example) is a constant and not a parameter. Here is my proposed solution for that (no closures!):

    var applyArrayFunctions = function(arr, x){ 
        for(var i=0; i<arr.length; i++) {
            x = arr[i](x);
        }
        return x;
    }
    
    var num = 2;
    var fnArray = [
        function(val){
          return val + 20; 
        },
        function(val){
          return val * 2; 
        }
    ];
    
    console.log(applyArrayFunctions(fnArray, num));​ // logs 44​​​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been a JavaScript developer for a while now, and I've always thought that
When I write client side code, I use HTML/CSS/JavaScript and lately jQuery to both
I am veteran server side java developer. I have little experience in client web
We are developing an occasionally connected application that uses SQL Compact on the client
While debugging a client app that uses a Google backend, I have added some
I'm a PHP web application developer that has built several large projects in PHP
I am a web developer, and I prefer to use a rich-client type of
While renewing iOS Developer program I have found Apple generated some Company ID instead
I have been working with SQL Server as a Developer a while. One thing
My developer tools session pauses while the page load. I see it happens while

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.