console.log有什么用?
请用一个代码示例解释如何在JavaScript中使用它。
console.log有什么用?
请用一个代码示例解释如何在JavaScript中使用它。
当前回答
当我开始console.log进行调试时,我真的觉得web编程很容易。
var i;
如果我想检查i运行时的值..
console.log(i);
您可以在firebug的console选项卡中检查I的当前值。它是专门用于调试。
其他回答
您可以使用它在Firefox的Firebug或WebKit浏览器的JavaScript控制台中调试JavaScript代码。
var variable;
console.log(variable);
它将显示变量的内容,即使它是一个数组或对象。
它类似于print_r($var);PHP。
当我开始console.log进行调试时,我真的觉得web编程很容易。
var i;
如果我想检查i运行时的值..
console.log(i);
您可以在firebug的console选项卡中检查I的当前值。它是专门用于调试。
jQuery没有什么可做的,如果你想使用它,我建议你去做
if (window.console) {
console.log("your message")
}
因此,当代码不可用时,您不会破坏代码。
正如评论中所建议的,您也可以在一个地方执行该命令,然后正常使用console.log
if (!window.console) { window.console = { log: function(){} }; }
这与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实际上可以接受任意多的参数。