console.log有什么用?

请用一个代码示例解释如何在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()方法将一条消息写入控制台并显示它。

~对于测试目的非常有用。

~在脚本标记下/内部使用它们。

怎么看:

= >按F12

语法:

Console.log(要显示的消息);

~接受一个参数并显示它。

使用Array:

var myArray = [“Ali”, “John”, “Shahrukh”]; console.log(myArray);

使用对象:

var myObject = {firstname: "Ali", lastname: "Rana"}; console.log (myObject);

使用文本:[方法1]:

console.log(“你好StackOverflow”);

使用文本:[方法2]

var str = “Hello StackOverflow”; console.log(str);

与数字打交道:

var myvar = '2'; console.log(myvar);

工作功能:

函数myfunction(){返回(5 * 19);} console.log(函数());

显示带有参数的消息:

Var a = 2; console.log(" a的值是" + a);

希望,也许会有所帮助。

console.log()是JavaScript中的一个函数,用于打印之前在其中定义的任何类型的变量,或者只是打印需要显示给用户的任何消息。

如代码:

function func() { return (5 * 18); }
     console.log(func());

IMG:

输出:

它不是jQuery特性,而是用于调试的特性。例如,当发生一些事情时,您可以将一些事情记录到控制台。例如:

$('#someButton').click(function() {
  console.log('#someButton was clicked');
  // do something
});

当你点击按钮时,你会看到#someButton在Firebug的“控制台”选项卡(或其他工具的控制台-例如Chrome的Web检查器)中被点击。

由于某些原因,控制台对象可能不可用。然后你可以检查它是否正确——这很有用,因为当你部署到生产环境时,你不必删除调试代码:

if (window.console && window.console.log) {
  // console is available
}

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

您可以使用它在Firefox的Firebug或WebKit浏览器的JavaScript控制台中调试JavaScript代码。

var variable;

console.log(variable);

它将显示变量的内容,即使它是一个数组或对象。

它类似于print_r($var);PHP。