---
title: "Implementing Private Fields for JavaScript"
description: "SpiderMonkey implements JavaScript private fields as hidden properties keyed by special PrivateName symbols, diverging from the specification's per-object [[PrivateFieldValues]] list so it can reuse the engine's already-optimized property machinery."
url: "https://swe.quest/articles/implementing-private-fields-for-javascript-8819b22a710ef7c1"
canonical: "https://hacks.mozilla.org/2021/06/implementing-private-fields-for-javascript/"
source: "Mozilla Hacks"
published: "2021-06-08"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 11
tags: ["runtimes","javascript","language-design","browsers"]
---

# Implementing Private Fields for JavaScript

A post on [Mozilla Hacks](https://swe.quest/sources/mozilla-hacks.md), published 2021-06-08, 11 min read, Deep dive. Original: [https://hacks.mozilla.org/2021/06/implementing-private-fields-for-javascript/](https://hacks.mozilla.org/2021/06/implementing-private-fields-for-javascript/)

## Summary

SpiderMonkey implements JavaScript private fields as hidden properties keyed by special PrivateName symbols, diverging from the specification's per-object \[\[PrivateFieldValues\]\] list so it can reuse the engine's already-optimized property machinery. The WeakMap mental model explains surprising semantics such as the constructor-override trick that stamps private fields onto arbitrary or sealed objects, and private fields living on a Proxy rather than its target. To preserve spec semantics, a new CheckPrivateField bytecode throws TypeErrors on missing or duplicate fields and is inline-cached via CacheIR using object Shapes. Private fields are also hidden from enumeration, and Proxies get a lazily allocated expando object to hold them. The feature ships in Firefox 90.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)
- [JavaScript](https://swe.quest/tags/javascript.md) (Languages)
- [Language design](https://swe.quest/tags/language-design.md) (Compilers & runtimes)
- [Browsers](https://swe.quest/tags/browsers.md) (Web & mobile)

## Prerequisites

- JavaScript classes ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))
- JS engine inline caches ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))
- object shapes/hidden classes
- JavaScript Proxies ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))

## Keywords

SpiderMonkey, Firefox, TC39, private fields, CacheIR, WeakMap, Proxy

## Related posts

- [Getting lively with Firefox 90](https://swe.quest/articles/getting-lively-with-firefox-90-e2cf4b92d9ecbff1.md): Mozilla Hacks · 2021-07-13 · Medium · 3 min read · Browsers, JavaScript, Frontend
  Firefox 90 enables JavaScript private class fields and methods by default, using a # prefix so properties can only be read or written inside the class body, and adds the relative-indexing at() method to Array, String and TypedArray, which accepts negative indexes to count from the end.
- [How Bun supports V8 APIs without using V8 (part 2)](https://swe.quest/articles/how-bun-supports-v8-apis-without-using-v8-part-2-d765ff746fe7a86a.md): Bun · 2024-11-05 · Hard · 18 min read · Runtimes & VMs, JavaScript, C & C++, Memory management
  Bun emulates V8's memory layout on top of JavaScriptCore so that precompiled native addons, which inline V8 functions assuming specific pointer layouts, keep working.
- [The File System API with Origin Private File System](https://swe.quest/articles/the-file-system-api-with-origin-private-file-system-d365ac14632ecee7.md): WebKit · 2022-02-14 · Medium · 6 min read · Browsers, JavaScript, Frontend
  WebKit now supports the File System Standard's origin private file system, a per-origin sandboxed storage area where web apps can create, read, write, move, enumerate and delete files and directories.
- [How Bun supports V8 APIs without using V8 (part 1)](https://swe.quest/articles/how-bun-supports-v8-apis-without-using-v8-part-1-ff8148b61cb465d8.md): Bun · 2024-09-30 · Hard · 19 min read · Runtimes & VMs, C & C++, JavaScript, Memory management
  Bun, which runs on JavaScriptCore, implements a growing subset of V8's C++ embedding API so Node.js native modules like cpu-features load without V8.
- [Turbocharging V8 with mutable heap numbers](https://swe.quest/articles/turbocharging-v8-with-mutable-heap-numbers-d32025b1d5ea1f1c.md): V8 · 2025-02-25 · Hard · 5 min read · Runtimes & VMs, Compilers, JavaScript, Performance engineering, Memory management
  V8 eliminated a performance cliff in JetStream2's async-fs benchmark caused by a custom Math.random whose \`seed\` lives in a ScriptContext slot.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
