簡體   English   中英

console.log(variable); VS console.log([variable]); /一個提供信息,另一個不提供?

[英]console.log( variable ); VS console.log( [variable] ); / One gives info the other doesn't?

在Chrome瀏覽器中,這將提供HTML標簽:

var image = document.getElementsById('image');

console.log(image);

在Chrome中,這提供了各種信息,例如clientWidth等:

console.log([image]);

為什么是這樣?

console.log是一種挑剔的方法。 由於沒有正式的規范(上次我檢查過),因此瀏覽器團隊決定實施它。

在第一個.log(image) Chrome.console似乎在HTMLElement上運行.toString (或等效方法),並為您提供字符串輸出。

在第二個.log([image]) Chrome.console將數組的內容作為索引對象/基元輸出。 它不會嘗試像處理log的第一級參數一樣處理數組的子級,因此它將為您提供實際的HTMLElement對象,而不是字符串輸出。

console.log(image);

這將在文檔中記錄對HTMLELement的直接引用,並顯示實際的<img> (或任何其他元素)標記。

console.log([image]);

在這里,您將Array作為參數傳遞,它將記錄一個數組。 該數組只是向您顯示HTMLElementObject

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM