zoukankan      html  css  js  c++  java
  • TypeError: can't convert console.log(...) to primitive type

    一、背景

    火狐浏览器提示这个错误,谷歌没有。

    二、出错代码

    1 var eventHandlers = {
    2       'succeeded': function(e){
    3             console.log('send success' + e.cause) 
    4        },
    5        'failed':    function(e){
    6             console.log('send failed,reason='+e.cause)
    7        }
    8 };

    三、原因

    火狐处理console.log('send success' + e.cause)的时候,会默认把e.cause转换成String类型,所以提示如题的错误。

    四、解决办法

    不适用字符串拼接的方式,直接打印。

    五、修订后的代码

     1 var eventHandlers = {
     2     'succeeded': function(e){
     3         console.log(e.cause),
     4         console.log('send success') 
     5     },
     6     'failed':    function(e){
     7         console.log('send failed,reason'),
     8         console.log(e.cause)
     9     }
    10 };    

    六、测试结果

    错误消除。

  • 相关阅读:
    String
    Map和Set
    js的栈与堆
    js的私有属性
    随便谈一谈原型
    前端页面优化提速
    nth-child和nth-of-type
    重复输出字符串
    闭包
    mongodb内嵌文档的查询
  • 原文地址:https://www.cnblogs.com/yoyotl/p/6346429.html
Copyright © 2011-2022 走看看