<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Duncan Dean</title>
    <description>The latest articles on DEV Community by Duncan Dean (@duncandean).</description>
    <link>https://dev.to/duncandean</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F171112%2Fdff15700-8738-45e6-8cc3-d997e2a1a58e.jpg</url>
      <title>DEV Community: Duncan Dean</title>
      <link>https://dev.to/duncandean</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/duncandean"/>
    <language>en</language>
    <item>
      <title>Getting Started with JavaScript Proxy</title>
      <dc:creator>Duncan Dean</dc:creator>
      <pubDate>Sun, 17 Nov 2019 20:04:02 +0000</pubDate>
      <link>https://dev.to/duncandean/getting-started-with-javascript-proxy-45m3</link>
      <guid>https://dev.to/duncandean/getting-started-with-javascript-proxy-45m3</guid>
      <description>&lt;p&gt;Have you ever wished you could hook into the process of a fundamental operation on an object in JavaScript and extend it? &lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;Proxy&lt;/code&gt; object, we can intercept and define custom behaviour for operations like property lookup, assignment, and function invocation. Effectively, this allows us to overload these operators.&lt;/p&gt;

&lt;h1&gt;
  
  
  Background
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Proxy&lt;/code&gt; was first introduced as a standard in &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-proxy-objects"&gt;the ES2015 specification&lt;/a&gt;. It resides under the idea of &lt;a href="https://en.wikipedia.org/wiki/Metaprogramming"&gt;metaprogramming&lt;/a&gt;, specifically &lt;em&gt;intercession&lt;/em&gt; (a type of reflection).&lt;/p&gt;

&lt;p&gt;There are three important constituents when dealing with &lt;code&gt;Proxy&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;handlers&lt;/strong&gt;: These are placeholder objects which contain &lt;em&gt;traps&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;traps&lt;/strong&gt;: These are methods we can use to hook into operations. They provide property access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;target&lt;/strong&gt;: The object that our proxy &lt;em&gt;virtualises&lt;/em&gt;. This is effectively the object that we are proxying access to.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's jump into a quick example.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Note that even though we never assigned a value to the property &lt;code&gt;something&lt;/code&gt;, 42 was returned when we accessed it. This is a result of setting the &lt;code&gt;get&lt;/code&gt; trap on our proxy. We essentially &lt;em&gt;overloaded&lt;/em&gt; the &lt;code&gt;.&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;Interestingly, we can create a crude observable of an object using a &lt;code&gt;Proxy&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here we use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect"&gt;Reflect API&lt;/a&gt; to ensure that we are still setting the actual property and not just calling &lt;code&gt;onChange()&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Proxy in the Wild
&lt;/h1&gt;

&lt;p&gt;Now that you understand some of the basics of &lt;code&gt;Proxy&lt;/code&gt; we can take a look at how it is being used in some open source libraries to achieve some pretty neat functionality.&lt;/p&gt;

&lt;p&gt;We'll take a look at &lt;a href="https://github.com/GoogleChromeLabs/comlink"&gt;comlink&lt;/a&gt;, a library that makes using WebWorkers much simpler without having to orchestrate all your messages between your worker and the main thread with &lt;code&gt;postMessage()&lt;/code&gt;. It operates like &lt;a href="https://en.wikipedia.org/wiki/Remote_procedure_call"&gt;RPC&lt;/a&gt;. You can expose an object with certain properties and methods from the WebWorker scope. These are wrapped as promises in the main thread scope that you can &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An example of comlink usage:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Under the hood, when we call &lt;code&gt;.wrap()&lt;/code&gt;, &lt;a href="https://github.com/GoogleChromeLabs/comlink/blob/96d11972f7e195babbc93cdf2bc75912aaee750a/src/comlink.ts#L206"&gt;a &lt;code&gt;Proxy&lt;/code&gt; is being created&lt;/a&gt; so that the fundamental operators can be overloaded to provide extra functionality.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Proxy&lt;/code&gt; has also been used extensively in popular UI frameworks to notify views of any model changes using the &lt;code&gt;.set()&lt;/code&gt; trap.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Proxy&lt;/code&gt; can be a powerful tool for intercession in JavaScript and has great power. Use it wisely. 😉 &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>proxy</category>
    </item>
  </channel>
</rss>
