简体   繁体   English

JavaScript 未捕获类型错误:无法将未定义或 null 转换为 object

[英]JavaScript Uncaught TypeError: Cannot convert undefined or null to object

I have a question,我有个问题,

// case 1
const stack = [];
[1,2,3].forEach(stack.push);

This will throw Error这将抛出错误

Uncaught TypeError: Cannot convert undefined or null to object
    at push (<anonymous>)
    at Array.forEach (<anonymous>)
    at <anonymous>:1:9

But this will be fine,不过这样就好了

// case 2
const stack = [];
[1,2,3].forEach(element => stack.push(element));
console.log(stack); // [1,2,3]

And if you bind stack with this referring to itself,如果你用this引用自身来绑定堆栈,

// case 3
const stack = [];
[1,2,3].forEach(stack.push.bind(stack));
console.log(stack); // (9) [1, 0, Array(3), 2, 1, Array(3), 3, 2, Array(3)]

It also works in another way.它也以另一种方式工作。

How can these happen?这些怎么可能发生? What is the difference between a method(case 1) and an arrow function(case 2)?方法(案例 1)和箭头函数(案例 2)有什么区别?

stack.push references Array.prototype.push . stack.push引用Array.prototype.push This:这个:

const stack = [];
[1,2,3].forEach(stack.push);

doesn't work because it's equivalent to:不起作用,因为它相当于:

const stack = [];
[1,2,3].forEach(Array.prototype.push);

or或者

const callback = Array.prototype.push;
callback(1, 0, [1, 2, 3]);
callback(2, 1, [1, 2, 3]);
callback(3, 2, [1, 2, 3]);

which doesn't work because there is no this context of the array to push to.这不起作用,因为没有要推送到的数组的this上下文。

Method 2 works because you're doing => stack.push(element) - .push is being called with a calling context of stack , rather than stack.push being passed as a callback.方法 2 之所以有效,是因为您正在执行=> stack.push(element) - .push是使用stack的调用上下文调用的,而不是stack.push作为回调传递的。 (When passed as a callback, the calling context gets lost unless you bind the function, which you do in the 3rd method). (当作为回调传递时,调用上下文会丢失,除非您在第三种方法中绑定 function)。

For another example:再举一个例子:

 const obj = { fn() { console.log('fn. this is:', this); } }; const callback = obj.fn; // this does not refer to the object now: callback(); // but this will: obj.fn();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法将未定义或null转换为对象 - Uncaught TypeError: Cannot convert undefined or null to object 类型错误:无法将未定义或空值转换为对象 javascript - TypeError : Cannot convert undefined or null to object javascript Javascript:TypeError:无法将未定义或null转换为对象 - Javascript: TypeError: Cannot convert undefined or null to object 类型错误:无法将未定义或 null 转换为 object javascript - TypeError: Cannot convert undefined or null to object javascript 未捕获的类型错误:无法在推送时将 undefined 或 null 转换为对象(<anonymous> ) - Uncaught TypeError: Cannot convert undefined or null to object at push (<anonymous>) 未捕获的类型错误:无法将未定义或 null 转换为 object React JS - Uncaught TypeError: Cannot convert undefined or null to object React JS 如何解决Uncaught TypeError:无法将undefined或null转换为对象 - how to solve Uncaught TypeError: Cannot convert undefined or null to object 如何解决这个“Uncaught TypeError:无法将undefined或null转换为object” - How to resolve this “ Uncaught TypeError: Cannot convert undefined or null to object ” json数据错误未捕获的TypeError:无法将未定义或null转换为对象 - json data error Uncaught TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为 object 反应 - Uncaught TypeError: Cannot convert undefined or null to object React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM