JavaScript调试代码 (JavaScript debugging the code)
Debugging is the process of finding mistakes or bugs in the program. There are several ways one can debug their JavaScript code. This article will walk you through the strict mode in JavaScript and exception handling.
调试是发现程序中错误或错误的过程。 一种可以调试其JavaScript代码的方法 。 本文将向您介绍JavaScript和异常处理中的严格模式 。
严格模式 (The Strict Mode)
JavaScript has an in built strict mode which when used, strictly checks the code for any kind of errors. Some of the flexibility that is usually allowed in the language is barred in the strict mode so that the user writes pure, clean error free code thereby preventing bugs from occuring in the first place. Let's look at some examples,
JavaScript具有内置的严格模式 ,该模式在使用时会严格检查代码是否存在任何类型的错误。 在严格模式下禁止使用该语言通常允许的某些灵活性,以便用户编写纯净,干净的无错误代码,从而从一开始就防止错误发生。 我们来看一些例子
//function definition
function print() {
for (i = 0; i < 3; i++) {
console.log(i);
}
}
//calling the function
print();
If you run this code, you'll get on the console,
如果您运行此代码,则会进入控制台,
0
1
2
Your code runs perfectly fine producing the correct output even though it had an error. Variable i was not declared anywhere and directly initialized and used. JavaScript reads your code and understands that you must have forgotten to declare i so it does that for you while executing the code. However, running the same code in strict mode,
即使有错误,您的代码也可以正常运行,并产生正确的输出。 变量i没有在任何地方声明,而是直接初始化和使用。 JavaScript会读取您的代码,并了解您一定忘记了声明i,因此它在执行代码时会为您这样做。 但是,在严格模式下运行相同的代码,
"use strict";
//function definition
function print() {
for (i = 0; i < 3; i++) {
console.log(i);
}
}
//calling the function
print();
Reports an error saying ReferenceError: i is not defined.
报告错误,指出ReferenceError:未定义。
function person(name){
this.name=name;
}
let fuzzy=person("Fuzzy");
Can you guess what's wrong with the above code?
您能猜出上面的代码有什么问题吗?
We have omitted the new keyword before person("Fuzzy") while declaring an object of person type. However, this code runs perfectly fine without producing any errors.
在声明人员类型的对象时,我们在person(“ Fuzzy”)之前省略了新关键字 。 但是,此代码运行得很好,不会产生任何错误。
But if we run this in strict mode,
但是如果我们在严格模式下运行 ,
"use strict";
function person(name){
this.name=name;
}
let fuzzy=person("Fuzzy");
We get an error saying TypeError:Cannot set property of 'name' undefined.
我们收到一条错误消息,提示TypeError:Cannot set property of'name'undefined。
strict mode disallows giving a function multiple parameters with the same name and removes certain problematic language features.
严格模式不允许为函数提供多个具有相同名称的参数,并删除某些有问题的语言功能。
Exception Handling
异常处理
It a mechanism through which code throws an exception when it runs into a program.
它是一种机制,代码在运行到程序中时会通过该机制引发异常。
An exception can be any undesired value that we have got because of several reasons. It does not necessarily imply that our code had an abrupt logic or incorrect syntax, it simply means we got something that we didn’t want and it could also be because of interaction with some foreign API. Exception jumps down to the first call that started the current execution therefore unwinding the call stack and throws away all the call context it encounters.
异常可能是由于多种原因而获得的任何不希望的值。 这不一定意味着我们的代码具有突然的逻辑或不正确的语法,仅表示我们得到了我们不想要的东西,也可能是由于与某些外部API的交互。 Exception跳转到开始当前执行的第一个调用,因此取消了调用堆栈,并丢弃了它遇到的所有调用上下文。
function promptDirection(question) {
let result = prompt(question);
if (result.toLowerCase() == 'left') return 'L';
if (result.toLowerCase() == 'right') return 'R';
throw new Error('Invalid directions: ' + result);
}
function Look() {
if (promptDirection('Which way?') == 'L') return 'a house';
else return 'Two angy beans';
}
try {
console.log('You see', look());
} catch (err) {
console.log('Something went wrong ' + err);
}
Output
输出量
Something went wrong ReferenceError: look is not defined
The throw keyword raises an exception and catching is done by wrapping a piece of code in a try block followed by the catch keyword.
throw关键字引发异常,通过将一段代码包装在try块中,然后加上catch关键字来完成捕获 。
翻译自: https://www.includehelp.com/code-snippets/debug-javascript-code.aspx