console.log有什么用?

请用一个代码示例解释如何在JavaScript中使用它。


当前回答

console.log():这个方法是众所周知的,也是最常见的,它可以输出任何类型的任何内容。传递字符串,数字,对象甚至html元素作为参数,它将为您打印相同的内容。

Ex.

console.log('Your print statements here!');
console.log('2nd parameter is type object', {type: 'object'});
console.log('can print with multiple parameters', 100001, {type: 'object'});

从这里找到更多关于控制台的例子:控制台

其他回答

console.log与jQuery无关。

它将消息记录到调试控制台,例如Firebug。

console.log与jQuery无关。它是由调试器(包括Chrome调试器和Firebug)提供的一个通用对象/方法,允许脚本将数据(或大多数情况下的对象)记录到JavaScript控制台。

它用于将(传递给它的任何内容)记录到Firebug控制台。主要用途是调试JavaScript代码。

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实际上可以接受任意多的参数。

使用console.log向页面添加调试信息。

许多人使用alert(hasNinjas)来实现这一目的,但是console.log(hasNinjas)更容易使用。使用警报弹出一个模态对话框,阻止用户界面。

编辑:我同意巴蒂斯特·佩尔内特和扬·汉尼克的观点,检查窗口是一个非常好的主意。首先定义Console,以便在没有可用的控制台时代码不会中断。