Matt Miller
Oct 25, 2021

--

Sometimes I am very tempted to just add this to a project, but of course you never know what it would do to third-party dependencies:

Object
.getOwnPropertyNames(Object.prototype)
.forEach((p) => delete Object.prototype[p]);

Even though I know I will probably never run into this scenario, it drives me crazy that we all use JavaScript plain objects as lookup dictionaries and make assumptions about property lookups:

type BlogPostsDictionary = Record<string, BlogPost>;
let postsBySlug: BlogPostsDictionary;
let slug: string;
let post: BlogPost = postsBySlug[slug];

Knowing in the back of my mind that if slug were "constructor", "hasOwnProperty", etc., then post would be, of all things, a function!

(Yes, of course there's always Object.create(null), but you'd never get to use shorthand {} — you would even need to wrap any instances of the { ... } spread operator syntax!)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Matt Miller
Matt Miller

No responses yet

Write a response