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

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

为什么 infinity 在尝试重新赋值时不报错,但它是不可赋值的?

var x = 3;

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

Infinity = 4; // no error

console.log(Infinity); // Infinity

如你所见here:

Infinity is a property of the global object, or in other words, a variable in global scope.

因为它是全局的 属性 您可以访问它并分配任何值。但是,将值分配给 non Writable 属性 不会引发任何异常,当您处于 not 严格模式 ,并且不会更改 属性.

的值

如果您使用 "strict mode",则会引发异常。

"use strict"

Infinity = 4;