ReferenceError和TypeError是两种不同的错误类型。
ReferenceError表示变量或函数未被定义,或试图访问不存在的对象属性。例如:
console.log(x); // ReferenceError: x is not defined foo(); // ReferenceError: foo is not defined var obj = {}; console.log(obj.prop); // ReferenceError: prop is not defined on obj
TypeError表示变量或函数类型不正确,或试图对非对象类型进行对象操作。例如:
var obj = null; obj.prop = 123; // TypeError: cannot set property 'prop' of null var str = "hello"; str(); // TypeError: str is not a function var num = 123; num.toUpperCase(); // TypeError: num.toUpperCase is not a function
因此,当在代码中遇到错误时,需要仔细查看错误信息并根据错误类型采取正确的纠正措施。