首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Errors: Unexpected type

信息

代码语言:javascript
复制
TypeError: "x" is (not) "y"

Examples:
TypeError: "x" is undefined
TypeError: "x" is null
TypeError: "undefined" is not an object
TypeError: "x" is not an object or null
TypeError: "x" is not a symbol

错误类型

TypeError.

哪里出错了?

有一个意想不到的类型。这会产生undefinednull值。

另外,必须提供某些方法,例如Object.create()Symbol.keyFor()需要特定的类型。

示例

无效的情况

代码语言:javascript
复制
// undefined and null cases on which the substring method won't work
var foo = undefined;
foo.substring(1); // TypeError: foo is undefined

var foo = null;
foo.substring(1); // TypeError: foo is null


// Certain methods might require a specific type
var foo = {}
Symbol.keyFor(foo); // TypeError: foo is not a symbol

var foo = 'bar'
Object.create(foo); // TypeError: "foo" is not an object or null

解决这个问题

例如,要修复空值指针undefinednull值,可以使用typeof运算符。

代码语言:javascript
复制
if (typeof foo !== 'undefined') {
  // Now we know that foo is defined, we are good to go.
}

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com