简体   繁体   English

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

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

In Chrome, this gives the HTML tag: 在Chrome浏览器中,这将提供HTML标签:

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

console.log(image);

In Chrome, this gives all kinds of info such as clientWidth etc: 在Chrome中,这提供了各种信息,例如clientWidth等:

console.log([image]);

Why is this? 为什么是这样?

console.log is a finicky method. console.log是一种挑剔的方法。 It's implemented however the browser team decides, since there's no formal spec ( last I checked ). 由于没有正式的规范(上次我检查过),因此浏览器团队决定实施它。

In the first .log(image) Chrome.console seems to run .toString (or equivalent) method on your HTMLElement and gives you the string output. 在第一个.log(image) Chrome.console似乎在HTMLElement上运行.toString (或等效方法),并为您提供字符串输出。

In the second .log([image]) Chrome.console outputs the contents of the array as indexed objects/primitives. 在第二个.log([image]) Chrome.console将数组的内容作为索引对象/基元输出。 It won't try to process the children of the array the same as a first-level argument to log , so it gives you the actual HTMLElement Object, not the string output. 它不会尝试像处理log的第一级参数一样处理数组的子级,因此它将为您提供实际的HTMLElement对象,而不是字符串输出。

console.log(image);

This logs the direct reference to the HTMLELement in your document, showing the actual <img> (or any other element) tag. 这将在文档中记录对HTMLELement的直接引用,并显示实际的<img> (或任何其他元素)标记。

console.log([image]);

Here you pass an Array as argument, which will log an array. 在这里,您将Array作为参数传递,它将记录一个数组。 The array simply shows you the Object of the HTMLElement . 该数组只是向您显示HTMLElementObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM