我有类似的JS代码:
const supportedRegions = ['usa', 'brazil']
const isSupportedRegion = region => supportedRegions.includes(region)
isSupportedRegion('brazil') // true
它工作正常。但是,如果我写了类似的东西,则会返回意外错误:
const supportedRegions = ['usa', 'brazil']
const isSupportedRegion = supportedRegions.includes
isSupportedRegion('brazil') // error: Uncaught TypeError: Cannot convert undefined or null to object at includes (<anonymous>) at <anonymous>:1:1
为什么说不能从undefined
或null
转换?变量isSupportedRegion
是一个函数!
typeof isSupportedRegion // "function"
isSupportedRegion // ƒ includes() { [native code] }
有人可以解释发生了什么吗?