Chapter 14. Metaprogramming

This chapter covers a number of advanced JavaScript features that are not commonly used in day-to-day programming but that may be valuable to programmers writing reusable libraries and of interest to anyone who wants to tinker with the details about how JavaScript objects behave.

Many of the features described here can loosely be described as “metaprogramming”: if regular programming is writing code to manipulate data, then metaprogramming is writing code to manipulate other code. In a dynamic language like JavaScript, the lines between programming and metaprogramming are blurry—even the simple ability to iterate over the properties of an object with a for/in loop might be considered “meta” by programmers accustomed to more static languages.

The metaprogramming topics covered in this chapter include:

  • §14.1 Controlling the enumerability, deleteability, and configurability of object properties

  • §14.2 Controlling the extensibility of objects, and creating “sealed” and “frozen” objects

  • §14.3 Querying and setting the prototypes of objects

  • §14.4 Fine-tuning the behavior of your types with well-known Symbols

  • §14.5 Creating DSLs (domain-specific languages) with template tag functions

  • §14.6 Probing objects with reflect methods

  • §14.7 Controlling object behavior with Proxy

14.1 Property Attributes

The properties of a JavaScript object have names and values, of course, but each property also has three associated attributes that specify how that ...

Get JavaScript: The Definitive Guide, 7th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.