2016-07-15 73 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()用於相同的結果

相關問題