【问题标题】:Uncaught TypeError: Immutable prototype object '#<Object>' cannot have their prototype set未捕获的类型错误:不可变原型对象 '#<Object>' 不能设置其原型
【发布时间】:2016-12-10 14:03:26
【问题描述】:

我有一个链接到 .htm 文件的 .js 文件,而 .js 文件中的唯一代码是以下行:

Object.setPrototypeOf(Object.prototype,{x:616});

在加载 .htm 页面时,“chrome 控制台”会显示错误:

Uncaught TypeError: Immutable prototype object '#<Object>' cannot have their prototype set

我以前从未见过这个错误,在这里或网上都找不到任何东西。

我假设该对象已被密封/冻结,因此进行了测试:

console.warn('sealed        ⇒',Object.isSealed(Object.prototype));      //  false
console.warn('frozen        ⇒',Object.isFrozen(Object.prototype));      //  false
console.warn('extensible    ⇒',Object.isExtensible(Object.prototype));  //  true

但这并没有说明问题,因此让我很困惑。这是在设置Object.prototype 的原型时第一次发生这种情况,我想知道我的 Chrome 浏览器是否自动更新了新功能或其他什么?

【问题讨论】:

    标签: javascript prototype typeerror


    【解决方案1】:

    这是 ES7(又名 ES2016)中的新功能。内置原型对象Object.prototype 现在是一个Immutable Prototype Exotic Objects,其[[prototype]] 内部插槽已锁定。

    应该是prevent proxies from being inserted in the global lookup mechanism,详情见this commit。它有recently been implemented in V8

    【讨论】:

    • 为什么不允许扩展覆盖这个?
    • @Pacerier “扩展”是什么意思?浏览器扩展?
    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2013-04-19
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多