JavaScript TypeError – 设置 getter-only 属性“x”
此 JavaScript 异常设置 getter-only 属性仅在严格模式下工作,并且在用户尝试为仅指定 getter 的属性设置新值时发生。
信息:
TypeError: Assignment to read-only properties is
not allowed in strict mode (Edge)
TypeError: setting getter-only property "x" (Firefox)
TypeError: Cannot set property "prop" of #
错误类型:
TypeError
错误原因:用户试图为仅定义了 getter 的属性设置新值。
示例 1:在此示例中,为对象定义了 getter 方法。无法直接访问属性。
HTML
HTML
输出:
TypeError: Cannot set property temp of
# which has only a getter
示例 2:在此示例中,为对象“Person”定义了 getter 方法。无法直接访问属性“年龄”。
HTML
输出:
TypeError: Cannot set property age of
# which has only a getter