toString()与String()的区别说明

数值,布尔值,对象和字符串都有一个toString()方法(该方法返回字符串的一个副本)。但Null和undefined值没有这个方法

在不知道要转换的值是不是Null还是undefined的情况下,还可以使用转型函数String(),这个函数能将任何类型的值转换为字符串。

null.toString()  //"Uncaught TypeError: Cannot read property 'toString' of null"

String(null)   // 'null'

posted on 2018-05-15 22:03  YKing_匆  阅读(120)  评论(0编辑  收藏  举报