浅析-js的alert()与console.log()区分

alert():

  1. 有壅塞作用,不点击肯定,后续代码没法继承实行

  2. alert()只能输出string,假如alert输出的是对象会自动挪用toString()要领

        e.g. alert([a,b,c]);//a,b,c
  3. alert不支撑多个参数的写法,只能输出第一个值

    e.g. alert(1,2,3);//1
    

console.log():

  1. 在打印台输出

  2. 能够打印任何范例的数据

       e.g. console.log([a,b,c]);//[a,b,c]
  3. 支撑多个参数的写法

    e.g. console.log(1,2,3)// 1 2 3   
    

关于原型链中的toString()输出题目:
  

let e1= {
        n : 1,
        valueOf : function(){
            return this.n + 4
        },
        toString : function(){
            return this.valueOf() + 5
        }
    }

    console.log('e1==0 : ', e1==0)
    console.log('+e1 : ', +e1)
    console.log('e1 : ', e1)
    console.log('e1.toString() : ', e1.toString())
    alert(e1)  //10

  运转效果:
《浅析-js的alert()与console.log()区分》

  缘由:console.log()能够打印任何范例的数据。而 alert() 只能输出string。假如alert输出是对象会自动挪用 toString() 要领。假如想 console.log() 输出的与alert雷同,须要挪用 toString() 。

    原文作者:Caraxiong
    原文地址: https://segmentfault.com/a/1190000009132082
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞