際際滷

際際滷Share a Scribd company logo
Oleksandr Skachkov
Itera
JavaScript Metaprogramming with ES2015 Proxy
ABOUT ME
 Iteras FrontEnd Tech Lead
 WebKit contributor & JS hacker:
 Arrow function
 Async iterator & generators
 Promise.finally
 Twitter: @alSkachkov
ES2015 PROXY
JAVASCRIPT METAPROGRAMMING WITH
 Metaprogramming before ES2015
 Metaprogramming with ES2015 Proxy
 Use cases
BACKBONE
Backbone my first MV* framework
BACKBONE
BACKBONE
BACKBONE
WRONG!!!!
BACKBONE
OK?
ANGULARJS
YES!!!!!!
ANGULARJS
YES!!!!!!
ANGULARJS
WRONG!!!!
YES!!!!!!
ANGULARJS
REACT
REACT
WRONG!!!!
REACT
VUE
WHY?
JavaScript Metaprogramming with ES 2015 Proxy
Metaprogramming
THEORY
Metaprogramming refers to a variety of ways a program
has knowledge of itself or can manipulate itself
stackoverflow
METAPROGRAMMING. HOW?
eval  the best strategy to do metaprogramming in JS :-)
METAPROGRAMMING. HOW?
Reflection  is key strategy to do metaprogramming:
 Introspection - read the structure of a program
 Self-modification - change program structure
 Intercession - change the semantics of some language operations
REFLECTION. INTROSPECTION
Object.keys()
Object.getOwnPropertyNames()
REFLECTION. SELF-MODIFICATION
REFLECTION. SELF-MODIFICATION
Object.seal()
Object.freeze()
Object.preventExtension()
REFLECTION. INTERCESSION
Is not supported by ES5
 Some browsers do __noSuchMethod__ (deprecated)
PROXY API
 Mirror-based architecture  Tom
Van Cutsem 2007
 Proxy API : Tom Van Cutsem and
Mark S. Miller 2010
MDN
ES2015 PROXY
PROXY
PROXY
PROXY
target - can be any sorts of objects, or another proxy
PROXY
handler - An object whose properties are functions which define the behavior
of the proxy when an operation is performed on it.
JavaScript Metaprogramming with ES 2015 Proxy
PROXY
traps: The methods that provide property access. This is analogous to the
concept of traps in operating systems
PROXY. HOW IT WORKS
PROXY. HOW IT WORKS
PROXY. HOW IT WORKS
PROXY. HOW IT WORKS
PROXY. HOW IT WORKS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. PROPERTY ACCESS
TRAPS. CALL AND CONSTRUCT
TRAPS. CALL AND CONSTRUCT
PROXY. INVARIANTS
Semantics that remain unchanged when implementing custom operations are
called invariants.
PROXY. REVOCABLE
PERFORMANCE
PERFORMANCE
PROXY SLOWER IN 3-5X TIMES
WHY PROXY IS SLOW?
WHY PROXY IS SLOW?
CAN WE USE ES2015 PROXY?
Yes we can
CAN WE USE ES2015 PROXY?
from 6.5
Not fully supported in
REACT
VUE
USAGE SCENARIOS
SYNC OBSERVABLE
Use set, deleteProperty traps
VALIDATION OR ACCESS CONTROL
Use set trap with check conditions
PROFILING OR LOGGING
Use get, set, deleteProperty traps
VIRTUAL OBJECT
Virtual objects are proxies that emulate other objects
without those objects needing to be in the same address
space.
Use set, get, defineProperty, deleteProperty traps
MORE CASES
Firefox uses ES2015 Proxy to protect Host object
MORE CASES
Firefox uses ES2015 Proxy to protect Host object
SUMMARY
 Powerful abstraction
 Adding functionality transparently
 Magic
 Slow in most of the browsers
QUESTIONS?

More Related Content

JavaScript Metaprogramming with ES 2015 Proxy

Editor's Notes

  • #27: TheObject.seal()method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable TheObject.freeze()method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed, it also prevents the prototype from being changed. TheObject.preventExtensions()method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object).
  • #53: invariants
  • #54: invariants