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 8428715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:07:54+00:00 2026-06-10T05:07:54+00:00

Observe the following: function array_map(array, callback) { for (var i = 0; i <

  • 0

Observe the following:

function array_map(array, callback) {
    for (var i = 0; i < array.length; i += 1) {
        callback(array[i]);
    }
}
var a = [], b = [];

array_map([1, 2, 3], function (x) { a.push(x); });
// just gives a = [1, 2, 3] as expected

// but why does this not work: ?
array_map([1, 2, 3], b.push);
// Chrome: a = [], Firefox: can't convert undefined to object

I do understand why this happens, namely: push is no longer bound to b (but to the global object) if you pass it to array_map directly. I don’t really understand why Chrome doesn’t give an error, at least Firefox seems to give some kind of error.

How can I detect if a function like this is passed to array_map to avoid these kinds of bugs?

I’m hoping there are advanced reflection techniques available to trace the origin of a function. For instance b.push.constructor gives Function, but that’s not what I’m looking for.

  • 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-10T05:07:56+00:00Added an answer on June 10, 2026 at 5:07 am

    I’m not sure what you expect there to happen. Array.prototype.map requires a function as second parameter, which returns a new value for every iteration.

    Passing in just a function reference (which you do in your second example) doesn’t tell the function what it has to do anyway. So you’re kinda expecting that .map() applies some black magic and calls the passed in method with the correct parameter, which it obviously, can’t do.

    I totally didn’t get that you wrote your own mapping function. However, your problem there is that you’re losing scope of that .push() function. Only if you call it on the Array / Object like xxx.push(), the this within the called function will correctly reference the target object. Once you just passed the reference, this will either point to global / window or undefined and won’t work anymore.

    So solve that issue you could call it like

    array_map([1, 2, 3], b.push.bind(b));
    

    which also would apply an ES5 function. You can’t really detect for it within the array_map(). A function is a function, your best shot would be to detect whether or not the passed in method is a native or not, but I wouldn’t recommend that.

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

Sidebar

Related Questions

Observe the following program written in Java (complete runnable version follows, but the important
I have the following prototype JavaScript code Event.observe( window, 'load', function() { Event.observe( 'agreement',
I have the following which doesn't work correctly: var canvas = new fabric.Canvas('canvas'); canvas.observe('mouse:down',
Im having an issue with using prototypes observe function and gaining access to this
This following piece of code is meant to create a multilevel array, print it,
I am using prototype and have the following javascript: function displayDefinition(term){ var term_id =
I have the following code to listen firefox quit-application event observe: function(subject, topic, data)
I have the following code snippet to enable extension after firefox quit, observe: function
I have the following code in a file called overlay.js: Event.observe(window, 'load', function() {
Have the following JS included in my HTML: document.observe('dom:loaded', function() { Event.addBehavior( { 'a.move-up:click':

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.