Blog

Uncaught TypeError: Cannot read properties of undefined (reading ‘call’)

Uncaught TypeError: Cannot read properties of undefined (reading ‘call’)

Este error me pasó al intentar llamar a .map desde un objeto en lugar de un array. Me costó darme cuenta porque el error es un poco críptico. Podemos emular lo que ocurre haciendo:

// Correcto: array
console.log("[].map", [].map)
console.log("[].map.call", [].map.call)
// Incorrecto: objeto
console.log("{}.map", {}.map)
console.log("{}.map.call", {}.map.call)

// Salida

[].map ƒ map() { [native code] }
[].map.call ƒ call() { [native code] }
{}.map undefined
Uncaught TypeError: Cannot read properties of undefined (reading 'call')

Como explica aquí,

var a = [], f = function() {};

esto

a.map(f);

es equivalente a

Array.prototype.map.call(a, f);