[英]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.