简体   繁体   English

为什么在 JavaScript 中分配 Infinity 不会引发错误,但不起作用?

[英]Why does assigning Infinity in JavaScript not throw an error, but not work?

Why doesn't infinity give an error when attempted to be reassigned, but is not assignable?为什么无穷大在尝试重新分配但不可分配时不给出错误?

var x = 3;

x = 1; // good, normal
true = 3; // error, normal

Infinity = 4; // no error

console.log(Infinity); // Infinity

As you can read here :你可以在这里阅读:

Infinity is a property of the global object, or in other words, a variable in global scope . Infinity 是全局 object 的属性,换句话说,是全局 scope 中的变量。

Since it's a global property you can access it and assign any value.由于它是一个全局属性,您可以访问它并分配任何值。 However assigning a value to a non Writable property doesn't raise any exception, when you are in a not strict mode , and doesn't change the value of the property.但是,当您处于严格模式时,将值分配给Writable 属性不会引发任何异常,并且不会更改属性的值。

If you use "strict mode", then an exception would be raised.如果您使用“严格模式”,则会引发异常。

 "use strict" Infinity = 4;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么分配给字符串的一部分(使用数组语法)不会在javascript中引发错误? - Why does assigning to part of a string (using array syntax) not throw an error in javascript? 为什么JavaScript中的“ Infinity == Infinity”变为真? - why does “Infinity==Infinity” in JavaScript becomes true? 在JavaScript中,为什么~~ Infinity评估为0? - In JavaScript, why does ~~Infinity evaluate to 0? 为什么此javascript会引发未定义的错误? - Why does this javascript throw an undefined error? 为什么这个javascript会抛出这个特殊错误? - Why does this javascript throw this particular error? 为什么 javascript collapseToStart() 会抛出错误 - Why does javascript collapseToStart() throw an error 为什么此正则表达式会引发JavaScript错误 - Why does this regexp throw a javascript error 为什么有效-> key:'嘿'->'嘿'并且没有抛出任何错误? - Why does it work -> key : 'hey' ->'hey' and doesnt throw any error? React 中的错误边界:为什么 'throw new Error('some message')' 失败而 'throw errorObj' 工作? - Error Boundary in React: why does 'throw new Error('some message')' fail and 'throw errorObj' work? 为什么三元运算符中的逗号会在JavaScript中引发语法错误? - Why does this comma inside a ternary operator throw a syntax error in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM