Skip to main content

Metamorphosis with Proxy and Reflect

  • Chapter
  • First Online:
JavaScript Next
  • 1695 Accesses

Abstract

No API can or should attempt to be complete. Depending on the use-case, and the problem space, we often find that we may need to add, remove, or even constraint behavior to an object, or a type. Historically our approach to this was leveraging JavaScript’s dynamic nature, and simply tack on the additional behavior we wanted to see on any object. Symbols can help in this regard, by giving us the guarantee of collision-free keys. However, in many cases it can feel a tad hackish, especially when we do not wish to permanently modify behavior of an object for the lifetime of our application.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    https://github.com/k1r0s/kaop

  2. 2.

    https://sinonjs.org/

  3. 3.

    Anyone remember eval??

  4. 4.

    https://en.wikipedia.org/wiki/Monkey_patch

  5. 5.

    https://rubyonrails.org/

  6. 6.

    https://en.wikipedia.org/wiki/Proxy_pattern

  7. 7.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler

  8. 8.

    You can see this for yourself simply by using for(const m of Reflect.ownKeys(Reflect)) { console.log(m); } and comparing the list with the list found at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler .

  9. 9.

    https://en.wikipedia.org/wiki/Singleton_pattern

  10. 10.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/construct

  11. 11.

    www.freeformatter.com/json-to-xml-converter.html

  12. 12.

    Our DSL is heavily influenced by https://github.com/oozcitak/xmlbuilder-js/wiki .

  13. 13.

    The elements array will be initialized with an empty object as its first entry.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2019 Raju Gandhi

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Gandhi, R. (2019). Metamorphosis with Proxy and Reflect. In: JavaScript Next. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5394-6_13

Download citation

Publish with us

Policies and ethics