调试JavaScript代码

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377339.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Delphi运算符及优先级

单目运算符 (最高优先级) 取变量或函数的地址(返回一个指针) not 逻辑取反或按位取反 乘除及按位运算符 * 相乘或集合交集 / 浮点相除 div 整数相除 mod 取模 (整数相除的余数) as 程序运行阶段类型转换 (RTTI运算符) and 逻辑或按位求和 shl 按位左移 shr 按位右移 加减运算符…

NotifyMyFrontEnd 函数背后的数据缓冲区(二)

message level 函数pq_putmessage调用 low level 函数 pq_putbytes,pq_putbytes调用 internal_putbytes。 从internal_putbyes上来看&#xff0c;就可以发现其数据发送的机制:有一个小技巧&#xff0c;如果数据缓冲区满了&#xff0c;就发送&#xff0c;否则就先堆在那儿。如果…

从源码角度剖析VC6下的内存分配与切割的运作

目录前言1、heap初始化2、第一次分配内存&#xff0c;计算真正区块大小3、new_region管理中心4、__sbh_alloc_new_group()切割第一次分配好的内存5、开始切割内存前言 malloc与free带来的内存管理是应付小区块的&#xff0c;即SBH(small block heap)&#xff0c;这点也可以从源…

windows常见命令整理(持续更新)

windows常见命令整理 1. 文件1.1. 实时显示文件 logfile.txt 中新添加的内容&#xff08;类似于linux tail -f&#xff09; 2. 网络2.1. netstat 3. 进程和任务3.1. tasklist &#xff08;用于列出当前运行的进程及其详细信息&#xff09;3.2. wmic &#xff08;用于执行各种系…

最长公共子序列求序列模板提_最长公共子序列

最长公共子序列求序列模板提Description: 描述&#xff1a; This question has been featured in interview rounds of Amazon, MakeMyTrip, VMWare etc. 这个问题在亚马逊&#xff0c;MakeMyTrip&#xff0c;VMWare等访谈轮次中都有介绍。 Problem statement: 问题陈述&…

洛必达法则使用条件

使用条件 1、分子分母同趋向于0或无穷大 。 2、分子分母在限定的区域内是否分别可导。 3、当两个条件都满足时&#xff0c;再求导并判断求导之后的极限是否存在&#xff1a;若存在&#xff0c;直接得到答案&#xff1b;若不存在&#xff0c;则说明此种未定式无法用洛必达法则解…

求根号m(巴比伦算法)

巴比伦算法是针对求根号m的近似值情况的&#xff0c;它的思想是这样的&#xff1a; 设根号mX0,则如果枚举有答案X(X<X0)&#xff0c;则m/X>X0,当精度要求不高的时候&#xff0c;我们可以看成Xm/XX0,而如果精度要求比较高&#xff0c;我们只需取X和m/X的平均值作为新的枚举…

Android面试题

http://blog.csdn.net/aomandeshangxiao/article/category/841452 http://www.cppblog.com/life02/category/18316.html转载于:https://www.cnblogs.com/DonkeyTomy/articles/2598673.html

r语言 分类变量 虚拟变量_R语言中的变量

r语言 分类变量 虚拟变量R语言| 变数 (R Language | Variables) In the previous tutorial, we have come across the basic information that stands as a pavement for understanding the R language in depth. Now moving future let us educate ourselves about the concep…

算法题复习(快排、链表、二分、哈希、双指针)

目录1、快速排序复习2、链表部分复习203. 移除链表元素707. 设计链表206. 反转链表142.环形链表 II3、二分法复习4、哈希法复习5、双指针复习**15. 三数之和****18. 四数之和****27. 移除元素****344. 反转字符串**,简单&#xff0c;双指针从两侧往中间靠拢&#xff0c;并随时s…

Cassandra1.2文档学习(7)—— 规划集群部署

数据参考&#xff1a;http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/architecturePlanningAbout_c.html 当规划一个Cassandra集群部署时&#xff0c;关于你初始存储的数据的数据量你应当有一个好的想法&#xff0c;并且对于…

虚拟机设置NAT

需要开启虚拟机网络相关服务&#xff0c; 安装虚拟网卡&#xff0c; 还有必须安装 VMware ToolsVMware虚拟机下实现NAT方式上网1. 把你的虚拟网卡VMnet8设置为自动获得IP、自动获得DNS服务器&#xff0c;启用。2. 把你虚拟机中操作系统的“本地连接”也设置为自动获得IP、自动获…

窗体震动 C# (不使用Timer控件,控制窗体震动)

private static Point plocation new Point(); public static void StartVibration(Form form)//Form 传入需要振动的窗体 { plocation form.Location; for (int i 1; i < 41; i)//41&#xff0c;可以理解为震动的时间。…

算法题复习(栈与队列、二叉树)

目录栈与队列栈用于匹配的问题队列用于堆二叉树系列深度遍历&#xff0c;递归与迭代层序遍历二叉树属性二叉树修改与构造二叉搜索树公共祖先二叉搜索树的修改与构造栈与队列 栈用于匹配的问题 20. 有效的括号 https://leetcode-cn.com/problems/valid-parentheses/ 不匹配的三…

bpsk_BPSK的完整形式是什么?

bpskBPSK&#xff1a;二进制相移键控 (BPSK: Binary Phase Shift Keying) BPSK is an abbreviation of "Binary Phase Shift Keying". BPSK是“二进制相移键控”的缩写 。 BPSK is also occasionally called phase reversal keying (PRK), or 2PSK, which is the el…

win7 下安装oracle 10g

oracle 10g 在win7下安装&#xff0c;提示程序异常终止&#xff0c;发生未知错误 在网上搜结果&#xff1a; 修改Oracle 10G\database\stage\prereq\db\refhost.xml 在 </SYSTEM> <CERTIFIED_SYSTEMS>后面添加 <!--Microsoft Windows 7--> <OPERAT…

poj 1703 Find them, Catch them

题目链接&#xff1a;http://poj.org/problem?id1703 题目大意&#xff1a;警察抓获N个罪犯&#xff0c;这些罪犯只可能属于两个团伙中的一个&#xff0c;现在给出M个条件&#xff08;D a b表示a和b不在同一团伙&#xff09;&#xff0c;对于每一个询问(A a b)确定a&#xff0…

双向a*搜索算法_双向搜索算法

双向a*搜索算法什么是双音搜索&#xff1f; (What is bitonic search?) Searching a bitonic array is known as bitonic search. An array is said to be bitonic if it has an increasing sequence of integers followed immediately by a decreasing sequence of integers.…

关于LRU缓存简单记录以及代码补全。

目录大概思路时间空间复杂度分析指针操作具体细节代码双向链表设计私有成员变量设计:构造函数和析构函数设计&#xff1a;get与put具体设计双向指针的具体细节添加到头节点函数删除尾节点函数删除节点函数删除节点函数感想今天面试考到LRU&#xff0c;太紧张了&#xff0c;完全…

码农干货系列【4】--图像识别之矩形区域搜索

简介 定位某个图片的矩形区域是非常有用的&#xff0c;这个可以通过手动的选择某个区域来实现定位&#xff0c;图片相关的软件都提供了这个功能&#xff1b;也可以像本篇一个通过程序来实现智能定位。前者会有误差&#xff0c;效率低下&#xff1b;后者选区精度高&#xff0c;效…