简体   繁体   English

CONSOLE.LOG();如何和调试JavaScript

[英]Console.log(); How to & Debugging javascript

Ok so I hope this is a question that isn't to basic for you guys. 好的,我希望这个问题对你们来说不是基本的。

I know enough jQuery to get myself into trouble, meaning I can grab elements and do stuff with it, write my own little functions for interactivity and such. 我知道足够的jQuery让自己陷入困境,这意味着我可以抓住元素并用它做些事情,编写我自己的小功能来实现交互性等等。 But then something doesn't go as expected, before I post questions to stackoverflow and get answers that make me slap myself in the forehead I would like to debug it myself and am sick of inserting alert(); 但是在我将问题发布到stackoverflow并获得让我在额头上拍打自己的答案之前,有些事情没有按预期进行,我想自己调试它并且厌倦了插入alert(); into my code. 进入我的代码。 In reading up on the subject there is mention of console.log(); 在阅读这个主题时,提到了console.log(); , console.info(); console.info(); and the such but I can not find any resource that explains how to use these in real world scenarios for debugging. 这样但我找不到任何解释如何在现实场景中使用这些资源进行调试的资源。

Do any of you know of a good resource or tutorial (not afraid to read a book) that can explain how to use these functions for the layman . 你们中的任何人都知道一个好的资源或教程(不怕读书)可以解释如何将这些功能用于外行 It seems that the tutorials and such I am finding are either way to advanced or just skim the surface and don't show you how to use them. 似乎我找到的教程和其他方法是高级或只是略过表面,而不是告诉你如何使用它们。 I understand I can insert console.log(); 我明白我可以插入console.log(); and it will spit out information in the console for firebug or element inspector . 它会在控制台中为firebugelement inspector吐出信息。 But what if my hand baked function is doing something unexpected somewhere up the line, how do I find the problem as the browser parses the javascript. 但是,如果我的手工烘焙功能在某个地方做了一些意想不到的事情,那么当浏览器解析javascript时如何找到问题呢。

Any help would be greatly appreciated as I feel learning this will help me understand what is going on in my code, so I can stop staring at the screen going “Why isn't this working, it worked in the jsfiddle !” 任何帮助将不胜感激,因为我觉得学习这将有助于我理解我的代码中发生了什么,所以我可以停止盯着屏幕“为什么这不起作用,它在jsfiddle !”

console.log() just takes whatever you pass to it and writes it to a console's log window. console.log()只接受传递给它的任何内容并将其写入控制台的日志窗口。 If you pass in an array, you'll be able to inspect the array's contents. 如果传入数组,则可以检查数组的内容。 Pass in an object, you can examine the object's attributes/methods. 传入一个对象,可以检查对象的属性/方法。 pass in a string, it'll log the string. 传入一个字符串,它将记录字符串。 Basically it's "document.write" but can intelligently take apart its arguments and write them out elsewhere. 基本上它是“document.write”,但可以智能地拆分其参数并将其写入其他地方。

It's useful to outputting occasional debugging information, but not particularly useful if you have a massive amount of debugging output. 输出偶尔的调试信息很有用,但如果你有大量的调试输出则不是特别有用。

To watch as a script's executing, you'd use a debugger instead, which allows you step through the code line-by-line. 要观察脚本的执行情况,您可以使用调试器,它允许您逐行逐步执行代码。 console.log's used when you need to display what some variable's contents were for later inspection, but do not want to interrupt execution. 当您需要显示某些变量的内容以供以后检查但不想中断执行时,使用console.log。

I like to add these functions in the head. 我喜欢在头脑中添加这些功能。

window.log=function(){if(this.console){console.log(Array.prototype.slice.call(arguments));}};
jQuery.fn.log=function (msg){console.log("%s: %o", msg,this);return this;};

Now log won't break IE I can enable it or disable it in one place I can log inline 现在日志不会破坏IE我可以启用它或在​​一个我可以内联登录的地方禁用它

$(".classname").log(); //show an array of all elements with classname class

Learn to use a javascript debugger. 学习使用javascript调试器。 Venkman (for Firefox) or the Web Inspector (part of Chome & Safari) are excellent tools for debugging what's going on. Venkman(适用于Firefox)或Web Inspector(Chome和Safari的一部分)是用于调试正在进行的操作的绝佳工具。

You can set breakpoints and interrogate the state of the machine as you're interacting with your script; 您可以在与脚本交互时设置断点并查询机器的状态; step through parts of your code to make sure everything is working as planned, etc. 逐步执行部分代码以确保一切按计划运行,等等。

Here is an excellent write up from WebMonkey on JavaScript Debugging for Beginners . 以下是WebMonkey在JavaScript Debugging for Beginners上精彩编写 It's a great place to start. 这是一个很好的起点。

本质上, console.log()允许您在您选择的javascript调试器中输出变量,而不是每次要检查某些内容时闪烁alert() ...此外,对于更复杂的对象,它将为您提供树视图以检查而不是必须将元素转换为字符串,如alert()

Breakpoints and especially conditional breakpoints are your friends. 断点,特别是条件断点是你的朋友。

Also you can write small assert like function which will check values and throw exceptions if needed in debug version of site (some variable is set to true or url has some parameter) 你也可以写一个小的断言函数,它会检查值并在站点的调试版本中需要时抛出异常(某些变量设置为true或url有一些参数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM