大网数据广告活动
Web 标准与前端开发

SyntaxError: property name __proto__ appears more than once in object literal

· 52CE 我爱测

The JavaScript exception "property name \_\_proto\_\_ appears more than once in object literal" occurs when an object literal contains multiple occurrences of the `__proto__` field, which is used to set the prototype of this new object.

The JavaScript exception "property name __proto__ appears more than once in object literal" occurs when an object literal contains multiple occurrences of the __proto__ field, which is used to set the prototype of this new object.

Message

SyntaxError: Duplicate __proto__ fields are not allowed in object literals (V8-based)
SyntaxError: property name __proto__ appears more than once in object literal (Firefox)
SyntaxError: Attempted to redefine __proto__ property. (Safari)

Error type

SyntaxError

What went wrong?

The __proto__ key, unlike other property keys, is a special syntax in an object literal. It is used to set the prototype of the object being created and is not allowed to appear more than once in an object literal. Note that this restriction only applies to the __proto__ prototype setter syntax: if it actually has the effect of creating a property called __proto__, then it can appear multiple times. See prototype setter for the exact syntax restrictions.

Worth noting that the __proto__ key in object literals is a special syntax and is not deprecated, unlike the Object.prototype.__proto__ accessor property.

Examples

Invalid cases

const obj = { __proto__: {}, __proto__: { a: 1 } };

Valid cases

// Only setting the prototype once
const obj = { __proto__: { a: 1 } };

// These syntaxes all create a property called "__proto__" and can coexist
// They would overwrite each other and the last one is actually used
const __proto__ = null;
const obj2 = {
  ["__proto__"]: {},
  __proto__,
  __proto__() {},
  get __proto__() {
    return 1;
  },
};

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。