Follow

Here's a fun challenge: in , how do you determine whether a given value is an object literal (or equivalently plain object)?

· · Web · 3 · 1 · 1

@joepie91 Object.getPrototypeOf(target) === Object.getPrototypeOf({}) ?

@miyuko Nope! This will break on cross-realm objects (eg. from a worker thread) and null-prototype objects

@joepie91 Pure instinct that I bet some Javascript lawyer will "well actually" me on, but:

x.__proto__ === {}.__proto__

@joepie91 create an error object, get the stack trace, then regexp the source code for {}

... or check that the prototype is Object. you could also use Object.getOwnPropertyDescriptors and check there isn't anything unusual

Sign in to participate in the conversation
Pixietown

Small server part of the pixie.town infrastructure. Registration is closed.