console.log有什么用?
请用一个代码示例解释如何在JavaScript中使用它。
console.log有什么用?
请用一个代码示例解释如何在JavaScript中使用它。
当前回答
console.log将调试信息记录到某些浏览器(安装了Firebug的Firefox, Chrome, IE8,任何安装了Firebug Lite的浏览器)的控制台。在Firefox上,它是一个非常强大的工具,允许您检查对象或HTML元素的布局或其他属性。它与jQuery无关,但在使用jQuery时,通常会做两件事:
为Firebug安装firerequery扩展。除了其他优点外,这使得jQuery对象的日志记录看起来更好。 创建一个更符合jQuery链接代码约定的包装器。
这通常是这样的意思:
$.fn.log = function() {
if (window.console && console.log) {
console.log(this);
}
return this;
}
你可以调用它,比如
$('foo.bar').find(':baz').log().hide();
轻松检查jQuery链内部。
其他回答
console.log与jQuery无关。
它将消息记录到调试控制台,例如Firebug。
A point of confusion sometimes is that to log a text message along with the contents of one of your objects using console.log, you have to pass each one of the two as a different argument. This means that you have to separate them by commas because if you were to use the + operator to concatenate the outputs, this would implicitly call the .toString() method of your object. This in most cases is not explicitly overriden and the default implementation inherited by Object doesn't provide any useful information.
在控制台尝试的例子:
>>> var myObj = {foo: 'bar'}
undefined
>>> console.log('myObj is: ', myObj);
myObj is: Object { foo= "bar"}
然而,如果你试图连接信息文本消息与对象的内容,你会得到:
>>> console.log('myObj is: ' + myObj);
myObj is: [object Object]
请记住,console。log实际上可以接受任意多的参数。
引用MDN文档
控制台-包含许多你可以调用来执行的方法 基本的调试任务,通常集中在各种日志记录上 值发送到浏览器的Web控制台。 到目前为止,最常用的方法是使用console.log 记录特定变量中包含的当前值。
如何使用Javascript?
let myString = 'Hello World';
console.log(myString);
还要记住控制台是web浏览器全局窗口对象的一部分。因此,以下内容在技术上也是正确的,但在实际场景中没有使用。
let myString = 'Hello World';
window.console.log(myString);
console.log与jQuery无关。它是由调试器(包括Chrome调试器和Firebug)提供的一个通用对象/方法,允许脚本将数据(或大多数情况下的对象)记录到JavaScript控制台。
在早期,JS调试是通过alert()函数执行的——现在这是一种过时的做法。
console.log()是一个函数,它在调试控制台(如Webkit或Firebug)上写入一条消息以记录日志。在浏览器中,你在屏幕上看不到任何东西。它将一条消息记录到调试控制台。它只在带有Firebug的Firefox和基于Webkit的浏览器(Chrome和Safari)中可用。它在所有IE版本中都不能很好地工作。
控制台对象是DOM的扩展。
console.log()应该仅在开发和调试期间在代码中使用。
将console.log()留在生产服务器上的javascript文件中被认为是一种坏习惯。