ReferenceError 和 TypeError的区别

ReferenceError 表示引用了不存在的变量或函数。通常是因为该变量或函数未被声明或未被定义。

举个例子:以下代码访问了变量 name,但是name 没有被定义,因此就会报 ReferenceError 的错误。

console.log(name)
//ReferenceError: name is not defined

TypeError 表示使用了错误的数据类型或调用了不存在的方法。

举个例子:以下 name 是一个字符串,调用了数组的方法,因此就会报 TypeError 的错误。

var name = 'ayetongzhi'
name.join(',')
TypeError: name.join is not a function

猜你喜欢

转载自blog.csdn.net/qq_42816270/article/details/129566720