"x"的含义不是函数,或者其返回值不可迭代.错误 [英] The meaning of "'x' is not a function or its return value is not iterable" error

查看:535
本文介绍了"x"的含义不是函数,或者其返回值不可迭代.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无意中目睹了这会在V8中导致错误(Chrome,Node.js等):

I accidentally witnessed that this causes an error in V8 (Chrome, Node.js, etc):

for (let val of Symbol()) { /*...*/ }

TypeError:Symbol不是函数或其返回值不可迭代

TypeError: Symbol is not a function or its return value is not iterable

似乎其他任何不可迭代的值(包括函数)都会引起另一个错误:

It appears that any other non-iterable value (including a function) causes another error:

for (let val of function () { throw 'never called' }) { /*...*/ }

TypeError :(中间值)不可迭代

TypeError: (intermediate value) is not iterable

参考指出,该错误特定于Chrome:

As the reference states, the error is specific to Chrome:

TypeError:"x"不是函数,或者其返回值不可迭代(Chrome)

TypeError: 'x' is not a function or its return value is not iterable (Chrome)

...

作为for…of的右侧或作为函数(例如Promise.all或TypedArray.from)的参数给出的值不是可迭代的对象.可迭代对象可以是内置的可迭代类型,例如Array,String或Map,生成器结果或实现可迭代协议的对象.

The value which is given as the right hand-side of for…of or as argument of a function such as Promise.all or TypedArray.from, is not an iterable object. An iterable can be a built-in iterable type such as Array, String or Map, a generator result, or an object implementing the iterable protocol.

似乎所有列出的内容都不希望接受函数而不是可迭代作为参数,因此目前尚不清楚为什么该错误将重点放在函数类型上.

It seems that none of listed things are expected to accept a function instead of iterable as an argument so it's unclear why the error puts emphasis on function type.

此错误有任何含义吗?在什么情况下is not a function注释在上下文中有意义?

Is there any meaning to this error? Are there circumstances under which is not a function remark makes sense in its context?

推荐答案

是的,错误消息的两个部分都有含义.对于您来说,Symbol()的返回值是不可迭代的,因此这是第二种选择.作为第一个选项的示例,只需采取一些非函数的方法:

Yes, there is meaning to both parts of the error message. In the case you have at hand, the return value of Symbol() is not iterable, so that's the second option. As an example for the first option, just take something that's not a function:

let NotAFunction = {};  // Or any other object.
for (let val of NotAFunction()) {}

给出:Uncaught TypeError: NotAFunction is not a function or its return value is not iterable.在这种情况下,显然NotAFunction不是函数;-)

gives: Uncaught TypeError: NotAFunction is not a function or its return value is not iterable. In this case, clearly, NotAFunction is not a function ;-)

我不知道为什么没有两个单独的错误消息,分别是根本不是一个函数"和它是一个函数并且被调用,但是它的返回类型不是可迭代的".可能是由于内部逻辑中实现for..of循环的某种东西使具有更细粒度的错误报告变得异常复杂-因此,组合的错误消息仅提及了导致循环不起作用的两个可能原因.

I don't know why there aren't two separate error messages for "it's not a function at all" and "it was a function and it's been called, but its return type wasn't iterable". Presumably something in the internal logic to implement for..of loops made it prohibitively complicated to have finer-grained error reporting -- so the combined error message simply mentions two possible reasons why the loop didn't work.

这篇关于"x"的含义不是函数,或者其返回值不可迭代.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆