码迷,mamicode.com
首页 > 编程语言 > 详细

javascript错误信息

时间:2019-03-14 13:42:58      阅读:509      评论:0      收藏:0      [点我收藏+]

标签:typeof   exp   特殊   obj   出错   error   hellip   pre   自定义   

ECMA-262 定义了下列 7 种错误类型:

  1. TypeError 类型错误
  2. ReferenceError 引用错误
  3. SyntaxError 语法错误
  4. Error 错误
  5. EvalError 全局错误
  6. RangeError 参数错误
  7. URIError  编码错误

1.TypeError类型错误

当传入函数的操作数参数的类型并非操作符或函数所预期的类型时,将抛出一个 TypeError 类型错误。

例如:

/*
*  当尝试调用一个像方法的值时,这个值并不是一个方法
*/
var foo = undefined;
foo();   //Uncaught TypeError: foo is not a function

/*
*  当尝试调用一个对象的方法时,输错了名字,也会触发这个错误提示
*/

var x = document.getElementByID(‘foo‘);  //Uncaught TypeError: document.getElementByID is not a function
/*
*  尝试读取 null 或者 undefined ,把它当成了对象
*/
var someVal = null;
console.log(someVal.foo);

 

2.ReferenceError 引用错误

ReferenceError: Invalid left-hand side in assignment

例如:

/*
*  尝试给不能赋值的东西赋值,引起错误
*  “left-hand side in assignment” 提及了等号左手边的部分,因为左手边包含不能赋值的东西
*/
function doSomething(){};
if(doSomething() = ‘somevalue‘){};   

 

3.SyntaxError语法错误

Uncaught SyntaxError: Invalid or unexpected token

原因是数据有特殊字符或缺失部分特殊字符。

[ ] { } ( ) 这几个符号不配对常常导致出错。检查所有的圆括号和方括号是否配对。行号指出的不仅是问题字符。

Unexpected / 跟正则表达式有关。此时行号通常是正确的。

Unexpected ; 对象或者数组字面量里面有个;通常引起这个错误,或者函数调用的参数列表里有个分号。此时的行号通常也是正确的。

4.Error错误

Error 是基类型,其他错误类型都继承自该类型。因此,所有错误类型共享了一组相同的属性(错误对象中的方法全是默认的对象方法) 。 Error 类型的错误很少见,如果有也是浏览器抛出的,这个基类型的主要目的是供开发人员抛出自定义错误。

 

5.Unexpected token o in JSON at position 1

使用ajax返回结果时,如果结果还是对象,使用JSON.parse(),就会报错:JSON.parse() 方法用于将一个 JSON 字符串转换为对象。

因为结果已经是对象了,不能再使用JSON.parse转成对象了。

JSON.parse() 方法用于将一个 JSON 字符串转换为对象。

var res = JSON.parse(res)

JSON.stringfy() 方法用于将对象、数组转换成字符串

 

    var json_str1=‘{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}‘                //json字符串
    var json_str2=‘{"name":"zhangsan","age":23,"email":"chentging@aliyun.com"}‘   //json字符串
    console.log(typeof(json_str1))  //string
    console.log(typeof(json_str2))  //string

    var objct_json1=JSON.parse(json_str1)   //type object    
    var objct_json2=JSON.parse(json_str2)   //type object
    console.log(objct_json1) //{1: 1, 2: 2, 3: {…}}
    console.log(objct_json2) //{name: "zhangsan", age: 23, email: "chentging@aliyun.com"}

 

javascript错误信息

标签:typeof   exp   特殊   obj   出错   error   hellip   pre   自定义   

原文地址:https://www.cnblogs.com/zjx304/p/9889226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!