javascript错误_JavaScript中的错误

javascript错误

JavaScript错误 (JavaScript Errors)

Errors are very common in programming and development. Having errors in your program is a validation of your accuracy and an opportunity to strengthen your concepts and learn even more. Every language has a specification for errors. If your code does something unusual, it catches that unusual activity and shows an error. It's important to be able to understand errors only then one can remove it.

错误在编程和开发中非常常见。 程序中有错误是对您准确性的验证,也是增强概念和学习更多知识的机会。 每种语言都有错误说明。 如果您的代码执行异常操作,它将捕获该异常活动并显示错误。 重要的是要理解错误,然后才能将其删除。

JavaScript中的错误类型 (Types of Errors in JavaScript)

There are 6 primary errors in JavaScript,

JavaScript有6个主要错误,

  1. EvalError:

    EvalError

    Raised when the eval() functions are used incorrectly.

    当eval()函数使用不正确时引发。

  2. RangeError:

    RangeError

    Raised when a numeric variable exceeds its allowed range.

    当数字变量超出其允许范围时引发。

  3. ReferenceError:

    ReferenceError

    Raised when an invalid reference is used.

    在使用无效引用时引发。

  4. SyntaxError:

    语法错误

    Raised when a syntax error occurs while parsing JavaScript code.

    在解析JavaScript代码时发生语法错误时引发。

  5. TypeError:

    TypeError

    Raised when the type of a variable is not as expected.

    在变量类型与预期不符时引发。

  6. URIError:

    URIError

    Raised when the encodeURI() or decodeURI() functions are used in an incorrect manner.

    以不正确的方式使用encodeURI()或decodeURI()函数时引发。

Reference Error, Syntax Error, and Type Error are the most common errors even to novice programmers and developers. Even experienced programmers encounter this error so let's look at some common examples of these errors, try to read and interpret the error and ultimately clean up our code leaving it error-free,

即使对于新手程序员和开发人员, 引用错误语法错误类型错误也是最常见的错误。 即使是经验丰富的程序员也会遇到此错误,因此让我们看一下这些错误的一些常见示例,尝试读取和解释该错误,并最终清理我们的代码,使其保持无错误状态,

function checkValue(value) {
    if (value > 3)
        console.log('Old value encountered');
    if (value < 0)
        console.log('Unacceptable value');
    else if (value > 0 && value < 3)
        console.log('New value obtained');
)

Before reading further or running this code on the console, carefully read the code line by line, top to bottom. We have made a very obvious syntax error. We ended our function with a ) instead of a }. Since this deviated from the natural grammar of JavaScript, invoking this function will give us a syntax error.

在继续阅读或在控制台上运行此代码之前,请从上至下逐行仔细阅读代码。 我们犯了一个非常明显的语法错误 。 我们以)而不是}结束函数。 由于这与JavaScript的自然语法有所不同,因此调用此函数将给我们带来语法错误。

checkValue(1.7);

Output

输出量

Uncaught SyntaxError: Unexpected token ')'

If you miss braces somewhere, use incorrect order of braces, or do something that is syntactically incorrect, you get a SyntaxError. Also, this error tells you what syntax you've spelled wrong. So all we need to identify is a ) and see where we have placed it, and modify it.

如果错过某处的括号,使用不正确的括号顺序或执行语法上不正确的操作,则会收到SyntaxError 。 另外,此错误告诉您拼写错误的语法。 因此,我们需要标识的只是一个),然后查看将其放置在何处并进行修改。

function checkValue(value) {
    if (value > 3)
        console.log('Old value encountered');
    if (value < 0)
        console.log('Unacceptable value');
    else if (value > 0 && value < 3)
        console.log('New value obtained');
}

checkValue(1.7);

Output

输出量

New Value obtained

Look at the following code,

看下面的代码,

var num=3;
num.toUpperCase();

Output

输出量

Uncaught TypeError: num.toUpperCase is not a function 
at <anonymous>:1:5

Now we get a TypeError because the type of num should have been a character or a string, not a number. This is logically stupid though since someone who knows num is initialized a number would not assume it to be a string and try to convert it to upper case. However, if during your program you did something to convert num from a string to a number and then tried to add that method onto it, you will get the same error.

现在我们得到一个TypeError,因为num的类型应该是字符或字符串,而不是数字。 这在逻辑上是愚蠢的,但是由于知道num的人已经初始化了一个数字,所以不会认为它是字符串,而是尝试将其转换为大写。 但是,如果在程序执行过程中做了一些将num从字符串转换为数字的尝试,然后尝试将该方法添加到该方法上,则会遇到相同的错误。

Let's look at another example of TypeError,

让我们看一下TypeError的另一个示例。

var t = 'bananas';
Object.create(t);

Output

输出量

 Uncaught TypeError: Object prototype may only be an Object or null: bananas
    at Function.create (<anonymous>)
    at <anonymous>:1:8

We're trying to create an object out of a string hence we get a type error,

我们试图用字符串创建对象,因此出现类型错误,

var t={
	name: 'bananas',
	price: '60rupees'
}

Object.create(t);

Output

输出量

}__proto__: 
    age: 20
    name: "fuzzy"
    __proto__: Object

Now our code runs perfectly fine.

现在我们的代码运行得很好。

Let's look at in which situations we can get a ReferenceError.

让我们看看在什么情况下可以得到ReferenceError

    s.substring()

Output:

输出:

Uncaught ReferenceError: s is not defined
    at <anonymous>:1:1

Since, s is not defined we get a reference error as we're trying to make an invalid reference to s.

由于未定义s,因此在尝试对s进行无效引用时会出现引用错误。

错误对象 (The Error Object)

The errors we deal with are coded within the language and an important feature through which we can dive deeper into how they’re designed is the Error object. The Error constructor creates an error object. Whenever your code goes wrong somewhere, an instance of the same Error object is thrown.

我们处理的错误是用该语言编码的,而重要的功能是Error对象,通过该功能我们可以更深入地研究错误的设计方式。 Error构造函数创建一个错误对象。 每当您的代码在某处出错时,都会引发同一Error对象的实例。

    Error;
    ƒ Error() { [native code] }

Just like every other object, it also has properties and methods attached to it,

就像其他对象一样,它也具有附加的属性和方法,

    Error.name;
    "Error"

You can also see this error object inside out by typing the following,

您还可以通过键入以下内容从内到外查看此错误对象,

    Error.prototype;

{name: "Error", message: "", constructor: ƒ, toString: ƒ}
    constructor: ƒ Error()
    message: ""
    name: "Error"
    toString: ƒ toString()
    __proto__: Object

Errors when exploited for unusual functioning of the program lead to bugs. We can use exception handling to identify errors and remove them. JavaScript also allows us to use the strict mode which is very helpful in identifying errors.

当利用的方案导致的错误不寻常的运作错误 。 我们可以使用异常处理来识别错误并将其删除。 JavaScript还允许我们使用严格模式,这对于识别错误非常有帮助。

翻译自: https://www.includehelp.com/code-snippets/errors-in-javascript.aspx

javascript错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值