console.log有什么用?

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


当前回答

这与jQuery无关。console.log()引用控制台对象的日志函数,该函数提供了将信息记录到浏览器控制台的方法。这些方法仅用于调试目的,不应依赖于将信息呈现给最终用户。

其他回答

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与jQuery无关。

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

我试着用简单的方式解释:

为什么可用:

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);

希望,也许会有所帮助。

log是开发人员编写代码的一种方法,它可以不引人注目地通知开发人员代码正在做什么。它可以用来提醒您存在问题,但在调试代码时不应该取代交互式调试器。它的异步性质意味着记录的值不一定代表调用方法时的值。

简而言之:使用console.log(如果可用)记录错误,然后使用您选择的调试器:Firebug, WebKit开发工具(Safari和Chrome内置),IE开发工具或Visual Studio修复错误。

在早期,JS调试是通过alert()函数执行的——现在这是一种过时的做法。

console.log()是一个函数,它在调试控制台(如Webkit或Firebug)上写入一条消息以记录日志。在浏览器中,你在屏幕上看不到任何东西。它将一条消息记录到调试控制台。它只在带有Firebug的Firefox和基于Webkit的浏览器(Chrome和Safari)中可用。它在所有IE版本中都不能很好地工作。

控制台对象是DOM的扩展。

console.log()应该仅在开发和调试期间在代码中使用。

将console.log()留在生产服务器上的javascript文件中被认为是一种坏习惯。