Skip to content

Instantly share code, notes, and snippets.

@tjjfvi
Last active October 24, 2022 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjjfvi/ea194c4fce76dacdd60a0943256332aa to your computer and use it in GitHub Desktop.
Save tjjfvi/ea194c4fce76dacdd60a0943256332aa to your computer and use it in GitHub Desktop.
Object Construction with `__proto__`

Object construction with __proto__ such as

let foo = { __proto__: null }
let bar = { __proto__: Bar.prototype, abc: 123 }

is perfectly standardized and safe, unlike the unsafe and legacy Object.prototype.__proto__ getter and setter methods.

The above code is equivalent to

let foo = Object.create(null);
let bar = { abc: 123 }
Object.setPrototypeOf(bar, Bar.prototype)

but faster[citation needed] and much more ergonomic.

Unfortunately, TypeScript does not currently understand this pattern so any uses of it must be @ts-ignored, preferably with a link to this gist or the relevant TypeScript issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment