console.log有什么用?

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


当前回答

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

为什么可用:

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

希望,也许会有所帮助。

其他回答

在java脚本中,没有输入和输出函数。因此使用console.log()方法来调试代码。它是一种日志记录方法。它将在控制台日志(开发工具)下打印。

在打开IE开发工具之前,它不会出现在IE8及以下版本。

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链内部。

注意:在您的产品代码中保留对控制台的调用将导致您的站点在Internet Explorer中崩溃。永远不要打开包装。参见:https://web.archive.org/web/20150908041020/blog.patspam.com/2009/the-curse-of-consolelog

console.log与jQuery无关。

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

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

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