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

调试JavaScript代码相关推荐

  1. VS2010Web默认的浏览器设置和VS里调试JavaScript代码的设置

    前言 重装系统后,VS调用的Web浏览器不是IE了,VS调式不了JavaScript代码了.这两天一直在试终于搞定了.这里查找的问题当然主要是VS里面调式JavaScript代码了. 第一种方式设置V ...

  2. vscode 调试参数_如何通过vscode运行调试javascript代码

    初次正式要写 javascript 相关的代码,想要用 vscode 直接编译 js 代码,但是发现没有那么简单,需要配置好 launch.json 文件,现已经在vscode上编译过去并且可以调试 ...

  3. VSCode配置调试JavaScript代码

    VSCode配置调试JavaScript代码 官网说明调试 1.创建配置 launch.json 文件 按照图示点击创建 LeetCode 是文件夹的名字,以文件夹配置 选择Node.js调试器 Le ...

  4. Webstorm 调试 JavaScript 代码

    通常前端项目的 JavaScript 代码调试是通过浏览器的开发者工具(右键菜单中点击 "检查")来完成.这种调试的方式的舒适度很差,显示页面的区域占了很大一块空间,留给调试窗口的 ...

  5. asp js单步调试_如何使用Chrome的控制台高效的调试Javascript代码?

    引言 在我们的日常开发中我们常常会遇到JavaScript的调试问题,而我们解决问题的传统解决方案就是使用大量的console.log或者console对象的其他方法,这会给我们带来很多不便,特别是遇 ...

  6. 使用chrome开发者工具调试JavaScript代码的三种常用方法

    对 JS 程序的调试,除了在 JS 程序中使用 alert().console.log() 方法跟踪和调试代码外,开发人员也会经常使用一些调试工具.最常用的 JS 调试工具就是一些主流的浏览器的调试工 ...

  7. f12弹出debug_调试Javascript代码(浏览器F12及VS中debugger关键字)

    目前,常用的浏览器IE.Chrome.Firefox都有相应的脚本调试功能.作为我们.NET 阵营,学会如何在IE中调试JS就足够了,在掌握了IE中的调试方法以后,Chrome和Firefox中的调试 ...

  8. IDEA调试JavaScript代码

    1.使用 插件 JetBrains IDE Support 这里提供的是CSDN下载,离线安装版,也可以自己去翻墙去谷歌的插件市场下载安装. 其他浏览器只要是使用谷歌浏览器内核的都可以安装使用,你要是 ...

  9. Visual studio 中调试ASP程序、Javascript 代码 - 刘稻博客

    2019独角兽企业重金招聘Python工程师标准>>> PS: 1.在调试asp时,要先附加进程(附加到:自动:与具体调试网站对应的 dllhost.exe 或 w3wp.exe . ...

最新文章

  1. 10条PyTorch避坑指南
  2. 多平台数据库客户端工具DBeaver
  3. 史上最详细 DevC++无法运行正则表达式
  4. $.ajax()参数详解及标准写法
  5. 计算机组成原理alu功能实现代码_计算机组成原理小课堂(3)——易错知识点...
  6. ncl 添加点shp文件_一:python读取shapefile文件
  7. PBR理论基础3:基于图像的光照(上)
  8. json 和 数组的区别
  9. .h和.cpp文件的区别 .
  10. 联想云计算机终端,联想云桌面
  11. IMSettings 1.5.1 发布,输入法设置工具
  12. win10卸载软件通过控制面板
  13. 论文Express | 谷歌DeepMind最新动作:使用强化对抗学习,理解绘画笔触
  14. 基于AT89S52单片机的蘑菇大棚环境监测系统论文(附录代码)
  15. 315再曝数据安全问题,短信钓鱼、App窃密等成焦点
  16. 深度学习中的偏差和方差
  17. android布局高度大于屏幕高度,Android CoordinatorLayout + AppbarLayout + Viewpager布局超过屏幕高度...
  18. 如何显示计算机中本地用户和组,Win10本地用户和组在哪里_Win10怎么打开本地用户和组?-192路由网...
  19. 造价者必熟的6个工程预算成本测算思路
  20. uniCloud中云函数、云对象、Schema表、openDB、JQL概述

热门文章

  1. 由于业务需求,我是如何在需要页面添加悬浮按钮进行切换并添加水印的
  2. javaScript学习笔记总结(一)
  3. mysql 8.0认证失败_解决mysql8.0因密码认证插件导致的链接不上
  4. CSS中越界问题经典解决方案
  5. js操作json方法总结
  6. 服务器端如何开启GZIP压缩功能
  7. 【HTML基础】表格和表单
  8. Box 'laravel/homestead' could not be found.
  9. 小程序 循环中有多个input,怎么获取每个input输入框的值
  10. 关于window.history.back()后退问题