Unusual behavior of the `map` function in Firefox's JavaScript

Here's an interesting observation regarding the behavior of the map function in Firefox.

In a particular error scenario on a web application, when Firebug pauses at the error, entering the following code into the Firebug console:

["a", "b", "c", "d"].map(function(val, i, arr) {
    return val + " " + i + " " + typeof arr;
});

results in an unexpected output:

["a undefined undefined",
"b undefined undefined", 
"c undefined undefined", 
"d undefined undefined"]

Interestingly, if the same code is executed in the Firebug console of a new blank tab during the error condition, it produces the expected output:

["a 0 object", 
"b 1 object", 
"c 2 object", 
"d 3 object"]

This discrepancy suggests that in the error scenario, the map function only calls the callback with one argument instead of the expected three arguments.

The official documentation by MDN states: (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map)

The callback should be invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

This raises the question of whether this unexpected behavior is triggered by the app forcing Firefox into a specific mode?

(Tested on Firefox 12.0)

Answer №1

It appears that there may be conflicting code in your application that is modifying the Array.prototype.map function – potentially an incomplete polyfill implementation? To check if this is the issue, input [].map into the console. If the output does not display function map() { [native code] }, then it is highly likely that this is causing the problem.

Answer №2

>>> ["x", "y", "z", "w"].map(function(val, i, arr) { return val + " at index " + i + ", type: " + typeof arr; });
["x at index 0, type: object", "y at index 1, type: object", "z at index 2, type: object", "w at index 3, type: object"]

Chrome 11. It seems like there may be a JavaScript library that is altering the Array.prototype.map method.

You can verify this by using the developer tools:

>>> Array.prototype.map.toString()
"function map() { [native code] }"

If you do not see the [native code] message, it means the function has been replaced with something else.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to change the name of a child object within a clone in three.js? (renaming child objects within clones)

I have designed a 3D model using different elements: ParentObject-001.name = 'ParentObject-001'; ChildObjectA-001.name = 'ChildObjectA-001'; ChildObjectB-001.name = 'ChildObjectB-001'; Then, I combined them with: ParentObject ...

The PHP Ajax code runs a for loop twice, but the data is only stored once during its execution

<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body onload="searchVideo();"> <script> var data_length = 2; for(var i=0; i<data_length; i+ ...

Moving objects with Three.JS

Seems like I might just be overlooking a simple solution here. I have a setup with a spotlight pointing backward from the camera towards a plane geometry, as well as some randomly scattered boxes. My goal is to create a basic top-down demo. I move the came ...

Navigating through an array of latitude and longitude pairs during an AJAX request

Just starting out with PHP/AJAX and I could use some guidance. Currently, I have a leaflet map where I'm attempting to link two AJAX calls together. The initial AJAX call retrieves data based on a selected country ISO code. Subsequently, I've ut ...

How to effectively combine css() and delay() in jQuery?

fid: https://jsfiddle.net/k13sazuz/ Is there a way to create a delay chain for CSS rules using jQuery? $('.two').css('background','blue').delay(11800).css('background','red'); ...

Tips on initiating a $http.get request every second using a service

I am working on an angular application that includes a $http.get request. While it currently functions properly, I need it to be executed every second in order to retrieve new data. Service: angular.module('adf.widget.liveCharts') .service(&a ...

How can I extract echoed information from PHP after submitting form data through jQuery Ajax?

I am looking to transfer a file from an input form to a php script using ajax and handle the response echoed by my php script. Here is my HTML form: <form id="fileUploadForm" method="POST" enctype="multipart/form-data"> <input name="fileToU ...

Unable to display data retrieved from JSON file

I am encountering an unusual issue while trying to retrieve elements from JSON in JavaScript. I fetch a JSON string from a URL using the following code: // Create Request HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"www.someurl ...

Running the `npm build` command does not automatically execute the "build" script specified in the package.json file

I am exploring a new approach for my module by utilizing npm build instead of relying on gulp / Grunt or other specialized build tools. "scripts": { "build": "node build.js" }, The content of my build.js file is simply: console.log('Hello') ...

What is the best way to include a text input as the last option in a Select input form field?

I would like to implement a select input feature where users can choose from predefined options, with the added functionality of including a text input field as the last option for users who prefer to enter their own category. How can I achieve this? Curr ...

Aligning the 'container-fluid' slideshow and video player

I'm struggling to center a video in a slick slider that is set as a 'container-fluid'. The slideshow and video display fine across the full width of the browser, but when I resize the browser window or view the site on a lower resolution, I ...

The element is not positioned correctly due to the absolute positioning

This is my first attempt at creating a jQuery plugin and I have almost got it working correctly. You can view the example here. I've been struggling for days to position an element as desired. The goal is to display some text with an optional downwar ...

Ways to retrieve row and column data from a datagrid

Below is the code snippet I am currently working with: import React from 'react' import Button from '@material-ui/core/Button'; import Checkbox from '@material-ui/core/Checkbox'; import { DataGrid } from '@material-ui/da ...

Problem with ngStyle: Error indicating that the expected '}' is missing

I encountered an error in the Chrome console when trying to interpret the code below: <div [ngStyle]="{'width': '100%'; 'height': '100%'; 'background-size': 'cover'; 'background-repeat&ap ...

Pinia: Is it better to invoke '{myStore}' or 'return myStore' within the return statement?

I'm currently working on integrating a Pinia store into a component of my Vue app and I'm puzzled by the need to return the store in { } instead of just returning it as is. Can someone clarify the distinction between return {foo} and return foo f ...

How to Use AJAX to Read a Text File in JavaScript

Having trouble with AJAX! I have successfully set up a marquee on my website, but now I want it to dynamically fetch the text from a text file. The goal is to read the text from the file (which contains only one line) and assign it to a global variable nam ...

Utilizing Jquery to locate a specific word within a sentence and then appending a span element around

I need help fixing a function that is supposed to find a specific word in a sentence and add a span element around it. Here is the code I have so far, but for some reason it's not working as expected. Can anyone spot the mistake? Thank you! ...

Retrieve the selected value from a radio button

I am attempting to display the chosen value in an input field when a radio button is selected. However, the functionality seems to be broken when I include bootstrap.min.js. You can find the code on JSFiddle. $('input:radio').click(function() ...

Are there any modules in Angular 8 that are used across various projects?

I am facing a challenge with managing two projects that share the same core functionality. These projects have identical layouts and pages, but certain components and modules are specific to each project. Currently, I maintain two separate Angular projects ...

Changing the size of shapes in cytoscape.js when using the dagre layout

My goal is to resize the shapes of the nodes based on the size of the node text. I tried following the steps outlined in https://github.com/cytoscape/cytoscape.js/issues/649, but encountered some issues: The shapes consistently end up with equal height a ...