You can inspect HTML nodes and JavaScript objects in more detail by using the console commands listed in Table 5-1. Type the command-line APIs interactively within the console.
If your scripts share the same function name as a Command-Line API function, the function in your scripts takes precedence.
The functions listed in Table 5-1 are regular JavaScript functions that are part of the Web Inspector environment. That means you can use them as you would any JavaScript function. For example, you can assign a chain of Console API commands to a variable to create a useful shorthand. Listing 5-1 shows how you can quickly see all event types attached to the selected node.
After defining this function, inspect the magnifying glass in the top-right corner of this webpage, and type evs() in the console. An array containing the string “click” is returned, because there is a click event listener attached to that element.
Of course, these functions shouldn’t be included in your website’s JavaScript files because they are not available in the browser environment. Only use these functions in the Web Inspector console. Console functions you can include in your scripts are described in Console API.
详解
1、console.assert(expression, object)
定义:Writes an error to the console when the evaluated expression is false.
译文:当计算表达式为假时,向控制台输出错误信息。
示例1:
function greaterThan(a,b) { console.assert(a > b, {"message":"a is not greater than b","a":a,"b":b}); } greaterThan(5,6);
result:
示例2:
var isFalse=false; console.assert(isFalse,"Output Info when evaluated expression is false");
result:
2、console.clear()
定义:Clears the console.If the Preserve log checkbox is enabled, console.clear() is disabled. However, pressing the clear console button () or typing the shortcut Ctrl+L while the Console is in focus still works.See Clearing the console for more information.
译文:清楚控制台信息。如果保护日志复选框被启用,则console.clear()被禁用。然而,当控制台的焦点仍在工作时,按明确的控制台按钮(明确控制台按钮)或输入快捷键Ctrl + L。
3、console.count(label)
定义:Writes the the number of times that count() has been invoked at the same line and with the same label.
译文:输出被调用相同的行和相同的标签的总次数。
示例1:
function login(name) { console.count(name + ' logged in'); }
result:
4、console.debug(object [, object, ...])
定义:Identical to console.log().
译文:与console.log()一样;
小结:功能与console.log()一样。
5、console.dir(object)
定义:Prints a JavaScript representation of the specified object. If the object being logged is an HTML element, then the properties of its DOM representation are printed.
译文:打印具体对象的JavaScript表示。如果被记录的对象是一个HTML元素,然后打印DOM表示的属性。
示例1:
console.dir(document)
result:
6、console.dirxml(object)