0

I registered a callback function(abc) for a forEach call. The function abc is defined as a closure and it seems it doesn't get called. I would really appreciate to know more about this.

Code:

function xyz(array)
{
  // do something

  function abc(value,index,origin_array){
    // do something
  }

  array.forEach(abc);

  //other stuff
}
12
  • 2
    Works for me... jsfiddle.net/sp20rdzv Commented Aug 17, 2015 at 18:28
  • you can do that by call a function inside of other function Commented Aug 17, 2015 at 18:29
  • 5
    You should give a complete, runnable example that reproduces your problem. Currently, you don't call xyz (so we don't know what the value of array is). Other than that, your code looks perfectly fine, so it's not possible to give you an answer. Commented Aug 17, 2015 at 18:31
  • 1
    You need to add MCVE: How to create a Minimal, Complete, and Verifiable example Commented Aug 17, 2015 at 18:32
  • 1
    @gfullam you saved my day.How can I accept your answer now? Commented Aug 17, 2015 at 18:35

1 Answer 1

1

A minimal, complete, and verifiable example

This works in modern browsers, but for what it's worth, array.forEach is not compatible with IE8 and older, if perhaps that is where you are testing this.

For legacy browser compatibility, use a standard for loop instead.

var numbers = [1, 2, 3];

function xyz(array) {
  console.log(array);
  
  function abc(value, index, origin_array){
    console.log(value);
  }

  array.forEach(abc);
}

xyz(numbers);
<p>Open your development console.</p>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.