Skip to curriculum
Jump to the editor

5.3. Empty an object in place

Easy
Topics
10–20 min

settings = {} gives you a new empty object, but every other part of the program still holds the old one, full of data. Sometimes the object itself must be emptied: a cache that several modules share, a form state object passed around by reference, a registry that a library keeps. Emptying it in place means deleting its properties one by one, and that raises real questions: which properties does a key list leave out, and what happens when one cannot be deleted?

Write clearObject(object) in clear-object.js. It removes every own property that can be removed and returns the same object.

Signature

JAVASCRIPT
function clearObject(object) // returns the same object
  • object: the object to empty, changed in place.
  • Returns object itself, not a copy.

Requirements

  1. Every own property is removed: string keys and symbol keys, enumerable and non-enumerable, data properties and getters/setters.
  2. The same object is returned, so every reference to it sees it empty.
  3. Getters and setters are removed without being called.
  4. The prototype is kept, so inherited methods still work, and the prototype's own properties are not touched.
  5. A property that cannot be deleted (non-configurable, or on a frozen or sealed object) stays where it is, and no error is thrown.
  6. It works on an object without a prototype (Object.create(null)), and on an object with its own hasOwnProperty property.
  7. It throws a TypeError when object is null, undefined or a primitive.

Examples

JAVASCRIPT
const cache = { a: 1, b: 2 };
const sameCache = cache;
clearObject(cache);         // returns cache
sameCache;                  // {}: the same object, now empty
JAVASCRIPT
const tag = Symbol("tag");
const object = { visible: 1, [tag]: 2 };
Object.defineProperty(object, "hidden", { value: 3, enumerable: false, configurable: true });
clearObject(object);        // all three are gone
JAVASCRIPT
const record = {};
Object.defineProperty(record, "id", { value: 7, enumerable: true, configurable: false });
record.note = "temp";
clearObject(record);        // { id: 7 }: id cannot be deleted; no error
clearObject(Object.freeze({ a: 1 })); // { a: 1 }, no error

Environment

  • Plain JavaScript in a browser page; the tests call the global clearObject from clear-object.js.
  • Every built-in is allowed. Arrays (whose length cannot be deleted) and functions are out of scope.

Hints

  1. Object.keys leaves out two kinds of keys. Which function lists every own key?
  2. What does delete do with a non-configurable property in strict mode, and is there a way to delete that reports failure instead?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…