2016-07-15 72 views
-3

以下是与输出为什么console.log和document.write为相同的代码提供不同的输出?

var myArray = ["one", "two","three", "four"]; 
var arr = [myArray]; 
console.log(arr);    //ouput - [Array[4]] 
window.document.write(arr);  //ouput - one,two,three,four 

为什么都行给出了不同的输出码? 在此先感谢。

+0

'document.write'调用'toString'方法,wheras控制台是一个供应商风格的东西。 –

+1

你可以至少试着让它看起来像你在谷歌搜索之前来到这里问 – RDardelet

回答

3

console.log将知道数组或对象或任何JavaScript数据的结构。 因此,它可以正确打印。

console.log(myArray) // ["one", "two", "three", "four"] 

虽然document.write将在其上调用(arr)toString()方法。所以它打印one,two,three,four

window.document.write(arr.toString() // one,two,three,four 
0

document.write()的调用的toString()在阵列上。

您可以使用与Array.toString()用于相同的结果

相关问题