首先举个例子看打印出来的结果是什么 document.write([1,2,3]); document.write({
name: 'xx'
}); document.write({ name: 'xx', toString:function(){ return '调用了toString'; } })
解析 1、document.write期望接收和输出字符串,当接收的参数不为字符串时,会调用参数的toString方法,将其转化成字符串输出
2、 数组调用toString()后,会将其中的元素用逗号拼接起来变成字符串
3、 一般的对象调用toString()后返回[object Object]
重写对象的toString()方法,就按照重写方法的返回值输出;