SyntaxError: property name __proto__ appears more than once in object literal
The JavaScript exception "property name __proto__ appears more than once in object literal" occurs when anobject literal contains multiple occurrences of the__proto__
field, which is used toset 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
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. Seeprototype setter for the exact syntax restrictions.
Worth noting that the__proto__
key in object literals is a special syntax and is not deprecated, unlike theObject.prototype.__proto__
accessor property.
Examples
Invalid cases
const obj = { __proto__: {}, __proto__: { a: 1 } };
Valid cases
// Only setting the prototype onceconst 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 usedconst __proto__ = null;const obj2 = { ["__proto__"]: {}, __proto__, __proto__() {}, get __proto__() { return 1; },};