zoukankan      html  css  js  c++  java
  • JavaScript 调试常见报错以及原因

    JavaScript 调试常见报错以及原因

    测试环境 chrome 版本 66.0.3359.170(正式版本) (64 位)

    TypeError 类型错误

    不是操作符所接受的数据类型。

      //-------- 把不是函数的值当做函数调用
      var foo = undefined;
      foo();
      // Uncaught TypeError: foo is not a function
      // foo 不是一个函数
    
      //-------- 调用对象中不存在的函数, 其实就是 undefined
      var x = document.getElementByID('foo');
      // Uncaught TypeError: document.getElementByID is not a function
      // 调用的值不是一个函数
    
      //-------- 调用未声明的方法
      lala();
      // Uncaught ReferenceError: lala is not defined
      // lala 没有定义
    
      //-------- 把 null 或 undefined 当成对象
      var someVal = null;
      someVal.foo;
      // Uncaught TypeError: Cannot read property 'foo' of null
      // 无法读取 null 的 foo 属性
    
      var someVal = undefined;
      someVal.foo;
      // Uncaught TypeError: Cannot read property 'foo' of undefined
      // 无法读取 undefined 的 foo 属性
    

    ReferenceError 引用错误

    尝试给不能赋值的变量进行赋值。

      //-------- 尝试给不能赋值的变量进行赋值。
      function doSomething(){};
      doSomething() = 'somevalue'
      // Uncaught ReferenceError: Invalid left-hand side in assignment
      // 赋值符的左侧无效
    

    RangeError 范围错误

    设定的值在该数据类型的范围内。如数字的范围、数组长度的范围。

      [].length = -1 // 数据的 length 不能小于 0
      [].length = undefined //
      // Uncaught RangeError: Invalid array length
      // 无效的数组长度
    

    SyntaxError 语法错误

    无法解析的代码。

      //-------- 拼接字符串,但是没有使用 + 号
      'ni' 'hao'
      // Uncaught SyntaxError: Unexpected string
      // 意料之外的字符串
    
      //-------- 没有使用成对的引号
      var str = 'ni hao
      // Uncaught SyntaxError: Invalid or unexpected toke
      // 无效或意料之外的标记
    
      //-------- 无效的正则
      var reg = /[/
      // Uncaught SyntaxError: Invalid regular expression: missing /
    
  • 相关阅读:
    【web安全】浅谈web安全之XSS
    【css】浅谈BFC
    nodejs笔记一--模块,全局process对象;
    逼真打字机效果;
    深入理解CSS3 animation的steps
    网址链接收藏
    用hoverclock插件实现HTML5时钟
    JS多种方法实现随机颜色;
    canvas实现跟随鼠标旋转的箭头
    封装insertAfter、addClass、格式化时间
  • 原文地址:https://www.cnblogs.com/daysme/p/9045407.html
Copyright © 2011-2022 走看看