<?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: Thomas Barrasso</title>
    <description>The latest articles on DEV Community by Thomas Barrasso (@tombarr).</description>
    <link>https://dev.to/tombarr</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%2F150464%2Fef558b1d-0755-4447-add5-f4621facc39d.jpeg</url>
      <title>DEV Community: Thomas Barrasso</title>
      <link>https://dev.to/tombarr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tombarr"/>
    <language>en</language>
    <item>
      <title>Scale Securely with Transit Gateway</title>
      <dc:creator>Thomas Barrasso</dc:creator>
      <pubDate>Mon, 02 Dec 2019 13:01:01 +0000</pubDate>
      <link>https://dev.to/tombarr/scale-securely-with-transit-gateway-16h9</link>
      <guid>https://dev.to/tombarr/scale-securely-with-transit-gateway-16h9</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/swlh/scale-securely-with-transit-gateway-198f600547e8?source=rss-784649f47696------2"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ylTPz0-Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/0%2AU_jZ17j5eqDjGwsh" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AWS Transit Gateway provides secure and scalable cloud connectivity to on-premise networks&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/swlh/scale-securely-with-transit-gateway-198f600547e8?source=rss-784649f47696------2"&gt;Continue reading on The Startup »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>aws</category>
      <category>technology</category>
      <category>networking</category>
    </item>
    <item>
      <title>Why RSpec Tests Fail (and How To Fix Them)</title>
      <dc:creator>Thomas Barrasso</dc:creator>
      <pubDate>Mon, 07 Oct 2019 22:56:28 +0000</pubDate>
      <link>https://dev.to/tombarr/why-rspec-tests-fail-and-how-to-fix-them-249j</link>
      <guid>https://dev.to/tombarr/why-rspec-tests-fail-and-how-to-fix-them-249j</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/better-programming/why-rspec-tests-fail-and-how-to-fix-them-402f1c7dce16?source=rss-784649f47696------2"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dpLuK0Gr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1041/1%2AYwoj4blnnZzAOTzkMlRL8Q.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Knowing how to troubleshoot tests is an essential software skill&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/better-programming/why-rspec-tests-fail-and-how-to-fix-them-402f1c7dce16?source=rss-784649f47696------2"&gt;Continue reading on Better Programming »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>rspec</category>
      <category>testing</category>
      <category>programming</category>
    </item>
    <item>
      <title>A practical guide to Javascript Proxy</title>
      <dc:creator>Thomas Barrasso</dc:creator>
      <pubDate>Thu, 16 May 2019 16:19:25 +0000</pubDate>
      <link>https://dev.to/tombarr/a-practical-guide-to-javascript-proxy-4cpa</link>
      <guid>https://dev.to/tombarr/a-practical-guide-to-javascript-proxy-4cpa</guid>
      <description>&lt;h1&gt;
  
  
  Introduction to Proxy
&lt;/h1&gt;

&lt;p&gt;With &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy"&gt;Proxy&lt;/a&gt;, &lt;a href="https://blog.bitsrc.io/a-practical-guide-to-es6-proxy-229079c3c2f0"&gt;you get a tiger object disguised as a cat object&lt;/a&gt;. Here are about half a dozen examples that I hope will convince you that Proxy provides powerful metaprogramming in Javascript.&lt;/p&gt;

&lt;p&gt;Although it is not as well known as other ES2015 features, Proxy has many uses including &lt;a href="https://hackernoon.com/introducing-javascript-es6-proxies-1327419ab413"&gt;operator overloading&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Mock_object"&gt;object mocking&lt;/a&gt;, &lt;a href="https://medium.com/dailyjs/how-to-use-javascript-proxies-for-fun-and-profit-365579d4a9f8"&gt;concise-yet-flexible API creation&lt;/a&gt;, &lt;a href="https://github.com/sindresorhus/on-change"&gt;Object on-change events&lt;/a&gt;, and even powers the &lt;a href="https://medium.com/@gustojs/vuejs-3-and-other-top-news-from-q-a-event-with-core-vue-devs-c9834946ae7b"&gt;internal reactivity system behind Vue.js 3&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;Proxy&lt;/code&gt; object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc). – MDN&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A Proxy is a placeholder object that “traps” invocations and operations made to its target object which it can then passthrough, no-op, or handle more elegantly. It creates an undetectable barrier around the &lt;strong&gt;target object&lt;/strong&gt; that redirects all operations to the &lt;strong&gt;handler object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A Proxy is created using the &lt;code&gt;new Proxy&lt;/code&gt; constructor which accepts two, required arguments: the target object and handler object.&lt;/p&gt;


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


&lt;p&gt;The simplest example of a functioning Proxy is one with a single trap, in this case, a get trap that always returns “42”.&lt;/p&gt;

&lt;p&gt;The result is an object that will return “42” for any property access operation. That means &lt;code&gt;target.x&lt;/code&gt;, &lt;code&gt;target['x']&lt;/code&gt;, &lt;code&gt;Reflect.get(target, 'x')&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;However, Proxy traps are certainly not limited to &lt;code&gt;get&lt;/code&gt; operations. It is only one of more than a dozen different traps.&lt;/p&gt;

&lt;h1&gt;
  
  
  Proxy Use Cases
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vjTviOTd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pzf1qa0ie1wk7cj65v0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vjTviOTd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pzf1qa0ie1wk7cj65v0l.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It may not be immediately apparent how such a simple pattern could be so widely used, but hopefully, after a few examples, it will become more clear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Default/ “Zero Values”
&lt;/h2&gt;

&lt;p&gt;In GoLang, there is the concept of &lt;a href="https://tour.golang.org/basics/12"&gt;zero values&lt;/a&gt; which are type-specific, implicit default struct values. The idea is to provide type-safe default primitives values, or in Gopher-speak, “give your structs a useful zero value!”&lt;/p&gt;

&lt;p&gt;Though different &lt;a href="https://en.wikipedia.org/wiki/Creational_pattern"&gt;creation patterns&lt;/a&gt; enable similar functionality, Javascript had no way to wrap an object with implicit initial values. The default value for an unset property in Javascript is &lt;code&gt;undefined&lt;/code&gt;. That is, until Proxy.&lt;/p&gt;


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


&lt;p&gt;This three-line function wraps a target object. If the property is set, it returns the property value (pass-through). Otherwise, it returns a default “zero value.” Technically, this approach isn’t implicit either but it could be if we extended &lt;code&gt;withZeroValue&lt;/code&gt; to support type-specific (rather than parameterized) zero values for Boolean (&lt;code&gt;false&lt;/code&gt;), Number (&lt;code&gt;0&lt;/code&gt;), String (&lt;code&gt;“”&lt;/code&gt;), Object (&lt;code&gt;{}&lt;/code&gt;), Array (&lt;code&gt;[]&lt;/code&gt;), etc.&lt;/p&gt;


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


&lt;p&gt;One place where this functionality might be useful is a coordinate system. Plotting libraries may automatically support 2D and 3D rendering based on the shape of the data. Rather than create two separate models, it might make sense to always include &lt;code&gt;z&lt;/code&gt; defaulted to zero rather than &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Negative Array Indices
&lt;/h2&gt;

&lt;p&gt;Getting the &lt;a href="https://medium.freecodecamp.org/the-first-shall-be-last-with-javascript-arrays-11172fe9c1e0"&gt;last element in an Array&lt;/a&gt; in Javascript is verbose, repetitive, and prone to off-by-one errors. That’s why there is a &lt;a href="https://github.com/keithamus/proposal-array-last"&gt;TC39 proposal&lt;/a&gt; that defines a convenience property, &lt;code&gt;Array.lastItem&lt;/code&gt;, to get and set the last element.&lt;/p&gt;

&lt;p&gt;Other languages like Python and Ruby make access to terminal elements easier with &lt;a href="https://rubyquicktips.com/post/996814716/use-negative-array-indices"&gt;negative array indices&lt;/a&gt;. For example, the last element can be accessed simply with &lt;code&gt;arr[-1]&lt;/code&gt; instead of &lt;code&gt;arr[arr.length-1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With Proxy, negative indices can also be used in Javascript.&lt;/p&gt;


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


&lt;p&gt;One important note is that traps including &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/get"&gt;&lt;code&gt;handler.get&lt;/code&gt;&lt;/a&gt; stringify all properties. For array access, we need to coerce property names into Numbers which can be done concisely with the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_%28%29"&gt;unary plus operator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;[-1]&lt;/code&gt; accesses the last element, &lt;code&gt;[-2]&lt;/code&gt; the second to last, and so on.&lt;/p&gt;


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


&lt;p&gt;There is even an npm package, &lt;a href="https://github.com/sindresorhus/negative-array"&gt;negative-array&lt;/a&gt;, that encapsulates this functionality more completely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hiding Properties
&lt;/h2&gt;

&lt;p&gt;Javascript has notoriously lacked private properties. &lt;code&gt;Symbol&lt;/code&gt; was &lt;a href="https://codeburst.io/a-practical-guide-to-es6-symbol-3fc90117c7ac"&gt;originally introduced to enable private properties&lt;/a&gt;, but later watered down with reflective methods like &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols"&gt;&lt;code&gt;Object.getOwnPropertySymbols&lt;/code&gt;&lt;/a&gt; that made them publicly discoverable.&lt;/p&gt;

&lt;p&gt;The longstanding convention has been to name private properties with a leading underscore, effectively marking them “do not touch.” Proxy offers a slightly better approach to masking such properties.&lt;/p&gt;


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


&lt;p&gt;The &lt;code&gt;hide&lt;/code&gt; function wraps a target object and makes properties that are prefixed with an underscore inaccessible from the in operator and from methods like &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames"&gt;&lt;code&gt;Object.getOwnPropertyNames&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;


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


&lt;p&gt;A more complete implementation would also include traps like &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/deleteProperty"&gt;&lt;code&gt;deleteProperty&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/defineProperty"&gt;&lt;code&gt;defineProperty&lt;/code&gt;&lt;/a&gt;. Apart from closures, this is probably the approach that is closest to truly-private properties as they are inaccessible from enumeration, cloning, access, or modification.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caching
&lt;/h2&gt;

&lt;p&gt;There are two hard problems in computer science: cache invalidation, naming things, and off-by-one errors.&lt;br&gt;
It is not uncommon to face difficulties synchronizing state between the client and server. Data can change over time, and it can be difficult to know exactly where to place the logic of when to re-sync.&lt;/p&gt;

&lt;p&gt;Proxy enables a new approach: wrap objects to invalidate (and resync) properties as necessary. All attempts to access a property first check against a cacheing strategy that decides to returns what’s currently in memory, or to take some other action.&lt;/p&gt;


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


&lt;p&gt;This function is oversimplified: it renders all properties on an object inaccessible after a certain period of time. However, it would not be difficult to extend this approach to set time-to-live (TTL) on a per-property basis and to update it after a a certain duration or number of accesses.&lt;/p&gt;


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


&lt;p&gt;This example simply renders the bank account balance inaccessible after 10 second. For more in-depth, real-world use cases there are several articles on &lt;a href="https://medium.com/@niwaa/using-es6-proxy-to-meta-program-in-javascript-implement-caching-logging-etc-577e253b3e05"&gt;Caching &amp;amp; Logging&lt;/a&gt; and &lt;a href="https://digitalist.se/blogg/create-client-side-cache-using-es6-proxy"&gt;Client-Side Caching using Proxy and &lt;code&gt;sessionStorage&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enums &amp;amp; Read-Only Views
&lt;/h2&gt;

&lt;p&gt;These examples come from Csaba Hellinger’s article on &lt;a href="http://dealwithjs.io/es6-features-10-use-cases-for-proxy/#anameenumaabetterenumtop"&gt;Proxy Use Cases&lt;/a&gt; and &lt;a href="https://hacks.mozilla.org/2015/07/es6-in-depth-proxies-and-reflect/"&gt;Mozilla Hacks&lt;/a&gt;. The approach is to wrap an object to prevent extension or modification. Although &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze"&gt;&lt;code&gt;Object.freeze&lt;/code&gt;&lt;/a&gt; now provides functionality to render an object read-only, it’s possible to extend this approach for better enum objects that throw errors accessing non-existent properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read-Only View
&lt;/h2&gt;


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


&lt;h2&gt;
  
  
  Enum View
&lt;/h2&gt;


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


&lt;p&gt;Now we can create an Object that throws an exception if you try to access non-exist properties, rather than returning &lt;code&gt;undefined&lt;/code&gt;. This makes it easier to catch and address issues early on.&lt;/p&gt;

&lt;p&gt;Our enum example is also the first example of &lt;strong&gt;proxies on proxies&lt;/strong&gt;, confirming that a proxy is a valid target object for another proxy. This facilitates code reuse through composition of Proxy functionality.&lt;/p&gt;


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


&lt;p&gt;This approach can be further extended to include “simulated methods” like &lt;a href="https://github.com/gergob/jsProxy/blob/master/03-enum-nameof.js"&gt;&lt;code&gt;nameOf&lt;/code&gt;&lt;/a&gt; that return the property name given an enum value, mimicking the behavior in languages like Javascript.&lt;/p&gt;

&lt;p&gt;While other frameworks and language supersets like TypeScript offer an &lt;a href="https://www.typescriptlang.org/docs/handbook/enums.html"&gt;enum type&lt;/a&gt;, this solution is unique in that it works with vanilla Javascript without special build tools or transpilers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operator Overload
&lt;/h2&gt;

&lt;p&gt;Perhaps the most fascinating Proxy use case syntactically is the ability to overload operators, like the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in"&gt;&lt;code&gt;in&lt;/code&gt; operator&lt;/a&gt; using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/has"&gt;&lt;code&gt;handler.has&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;in&lt;/code&gt; operator is designed to check if a “specified property is in the specified object or its prototype chain.” But it is also the most syntactically-elegant operator to overload. This example defines a continuous &lt;code&gt;range&lt;/code&gt; function to compare Numbers against.&lt;/p&gt;


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


&lt;p&gt;Unlike Python, which uses generators to compare against a finite sequence of whole numbers, this approach supports decimal comparison and could be extended to support other numeric ranges–inclusive, exclusive, natural, rational, imaginary, ad infinitum.&lt;/p&gt;


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


&lt;p&gt;Even though this use case does not solve a complex problem, it does provide clean, readable, and reusable code. 🔥&lt;/p&gt;

&lt;p&gt;In addition to the &lt;code&gt;in&lt;/code&gt; operator, we can also overload &lt;code&gt;delete&lt;/code&gt; and &lt;code&gt;new&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cookies Object
&lt;/h2&gt;

&lt;p&gt;If you have ever had to interface with cookies in Javascript, you have had to deal with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie"&gt;&lt;code&gt;document.cookies&lt;/code&gt;&lt;/a&gt;. It is an unusual API in that the API is a String that reads out all cookies, semi-colon delimited but you use the assignment operator to initialize or overwrite a single cookie.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.cookies&lt;/code&gt; is a String that looks like:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_octo=GH1.2.2591.47507; _ga=GA1.1.62208.4087; has_recent_activity=1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In short, dealing with document.cookies is frustrating and error prone. One approach is the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie/Simple_document.cookie_framework"&gt;simple cookie framework&lt;/a&gt;, which can be adapted to use Proxy.&lt;/p&gt;


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



&lt;p&gt;This function returns an object that acts like any other key-value object, but proxies all changes to &lt;code&gt;document.cookies&lt;/code&gt; for persistence.&lt;/p&gt;


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


&lt;p&gt;In 11 lines, we have a better interface for modifying cookies, although additional features like string normalization would be necessary in a production environment.&lt;/p&gt;

&lt;h1&gt;
  
  
  Fine Print
&lt;/h1&gt;

&lt;p&gt;The devil is in the detail, and Proxy is no exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2YHCipoR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zmlc93jtj7xwxyyktksu.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2YHCipoR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zmlc93jtj7xwxyyktksu.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Polyfill
&lt;/h2&gt;

&lt;p&gt;At the time of writing (May 2019), there is no complete polyfill for Proxy. There is, however, a partial polyfill for Proxy written by Google that supports the &lt;code&gt;get&lt;/code&gt;, &lt;code&gt;set&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, and &lt;code&gt;construct&lt;/code&gt; traps and works for IE9+.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is it a Proxy?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;It is impossible to determine whether an object is a proxy or not. – 2ality&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to the Javascript language specifications, &lt;strong&gt;there is no way to determine if an Object is a Proxy&lt;/strong&gt;. However, on Node 10+ it is possible using the &lt;a href="https://nodejs.org/api/util.html#util_util_types_isproxy_value"&gt;&lt;code&gt;util.types.isProxy&lt;/code&gt;&lt;/a&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  What’s the target?
&lt;/h2&gt;

&lt;p&gt;Given a Proxy object, &lt;strong&gt;it’s not possible to obtain or change the target object&lt;/strong&gt;. It is also not possible to obtain or modify the handler object.&lt;/p&gt;

&lt;p&gt;The closest approximation is in Ben Nadel’s article &lt;a href="https://www.bennadel.com/blog/3262-using-proxy-objects-to-dynamically-change-the-this-binding-within-a-generator-in-node-js.htm"&gt;Using Proxy to Dynamically Change THIS Binding&lt;/a&gt;, which uses an empty object as the Proxy target and closures to cleverly reassign the object the Proxy’s actions are performed on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proxy Primitives
&lt;/h2&gt;

&lt;p&gt;Unfortunately, one limitation of Proxy is that &lt;strong&gt;the target has to be an Object&lt;/strong&gt;. That means we cannot use primitives like String directly. 😞&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://thecodebarbarian.com/thoughts-on-es6-proxies-performance"&gt;A major drawback of Proxy is performance&lt;/a&gt;. Impact will vary based on browser and use, but &lt;strong&gt;Proxy isn’t the best approach for performance-critical code&lt;/strong&gt;. Of course you can always measure the impact and decide if the advantages of Proxy outweigh the affect on performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Proxy?
&lt;/h2&gt;

&lt;p&gt;Proxy provides a virtualized interface to control the behavior of any target Object. In doing so, it strikes a balance between simplicity and utility without sacrificing compatibility. Any code that expects an Object can accept a Proxy.&lt;/p&gt;

&lt;p&gt;Perhaps the most compelling reason to use Proxy is that many of the examples above are just a few lines long and can easily be composed to create complex functionality. For one final example, we can compose functions from several use cases to create a read-only cookie object that returns a default value for non-existent or “private”, hidden cookies.&lt;/p&gt;


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


&lt;p&gt;I hope these examples have shown that Proxy is more than just an esoteric feature for niche metaprogramming in Javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://dealwithjs.io/es6-features-10-use-cases-for-proxy/"&gt;10 Use Cases for Proxy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy"&gt;MDN–Proxy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://davidwalsh.name/javascript-proxy"&gt;David Walsh–JavaScript Proxy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.campvanilla.com/advanced-guide-javascript-proxy-objects-introduction-301c0fce9432"&gt;Intro to Javascript Proxy Object&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://2ality.com/2014/12/es6-proxies.html"&gt;2ality–Metaprogramming with ES6 proxies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>software</category>
    </item>
    <item>
      <title>Symbol.iterator for modular iteration without cloning or modifying in-place</title>
      <dc:creator>Thomas Barrasso</dc:creator>
      <pubDate>Thu, 11 Apr 2019 00:50:07 +0000</pubDate>
      <link>https://dev.to/tombarr/symbol-iterator-for-modular-iteration-without-cloning-or-modifying-in-place-1hji</link>
      <guid>https://dev.to/tombarr/symbol-iterator-for-modular-iteration-without-cloning-or-modifying-in-place-1hji</guid>
      <description>&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols"&gt;&lt;code&gt;Symbol.iterator&lt;/code&gt;&lt;/a&gt; is &lt;em&gt;the&lt;/em&gt; protocol that makes native objects like &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt;, and &lt;code&gt;Map&lt;/code&gt; iterable. It provides a hook into language features like the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator"&gt;spread operator&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of"&gt;&lt;code&gt;for…of&lt;/code&gt;&lt;/a&gt; loops.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;Symbol.iterator&lt;/code&gt;, developers can &lt;strong&gt;redefine navigation patterns without cloning or modifying an object in-place&lt;/strong&gt;. That means iterating &lt;a href="https://medium.com/front-end-weekly/thank-u-symbol-iterator-next-aef9f09ff78"&gt;forwards, backwards, randomly, infinitely, pairwise&lt;/a&gt;, or just about any way you can imagine.&lt;/p&gt;

&lt;p&gt;The protocol specification is very simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Iterable&lt;/strong&gt;: the object with a function whose key is &lt;code&gt;Symbol.iterator&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterator&lt;/strong&gt;: the iterable function, used to obtain the values to be iterated&lt;/li&gt;
&lt;/ul&gt;


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


&lt;p&gt;These two properties tell the Javascript runtime what the next &lt;strong&gt;value&lt;/strong&gt; is and when iteration is &lt;strong&gt;done&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider this example for iterating in reverse:&lt;/p&gt;


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


&lt;p&gt;Instead of iterating backwards using indices or &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse"&gt;&lt;code&gt;Array.reverse&lt;/code&gt;&lt;/a&gt;, we have defined the “going backwards” behavior for any array. We have done so while preserving the order and without creating a clone of the original array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;reverse&lt;/code&gt; can now be used in &lt;code&gt;for...of&lt;/code&gt; loops or with the spread operator:&lt;/p&gt;


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


&lt;p&gt;&lt;code&gt;Symbol.iterator&lt;/code&gt; can also be used to recreate Python's &lt;code&gt;range&lt;/code&gt; function.&lt;/p&gt;


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


&lt;p&gt;Now we can elegantly iterate over a range of numbers &lt;strong&gt;without needing to create an intermediate array&lt;/strong&gt;.&lt;/p&gt;


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


&lt;p&gt;It even seems that &lt;a href="https://jsperf.com/for-vs-range"&gt;performance is comparable to a standard for loop&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We do not even need to limit ourselves to a single collection. Like many libraries, we can write a &lt;code&gt;zip&lt;/code&gt; function that combines multiple sequential collections of equal length, &lt;em&gt;without needing to clone them in their entirety&lt;/em&gt;.&lt;/p&gt;


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


&lt;p&gt;This allows us to seamlessly iterate pairwise (or triplet-wise):&lt;/p&gt;


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


&lt;p&gt;In this case, &lt;a href="https://jsperf.com/js-zip"&gt;&lt;code&gt;Symbol.iterator&lt;/code&gt; significantly outperforms&lt;/a&gt; an cloning implementation using &lt;code&gt;Array.map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are &lt;a href="https://medium.com/front-end-weekly/thank-u-symbol-iterator-next-aef9f09ff78"&gt;many uses for &lt;code&gt;Symbol.iterator&lt;/code&gt;&lt;/a&gt;, and while it is more verbose than &lt;code&gt;Array.map&lt;/code&gt;, it is more versatile and potentially more efficient.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>TC39 proposal: Array.lastItem</title>
      <dc:creator>Thomas Barrasso</dc:creator>
      <pubDate>Thu, 28 Mar 2019 20:46:57 +0000</pubDate>
      <link>https://dev.to/tombarr/tc39-proposal-array-lastitem-3fb0</link>
      <guid>https://dev.to/tombarr/tc39-proposal-array-lastitem-3fb0</guid>
      <description>&lt;p&gt;A &lt;a href="https://github.com/keithamus/proposal-array-last"&gt;recent TC39 proposal&lt;/a&gt; offers easier access to the last element in a JavaScript Array. While on the surface this does not seem significant, there are &lt;a href="https://medium.freecodecamp.org/the-first-shall-be-last-with-javascript-arrays-11172fe9c1e0"&gt;many benefits&lt;/a&gt; to such a proposal.&lt;/p&gt;

&lt;p&gt;Terminal elements are unique in that they tend to be accessed more often than middle elements. Because of this, many other scripting languages like &lt;a href="https://stackoverflow.com/questions/18212240/ruby-convention-for-accessing-first-last-element-in-array"&gt;Ruby&lt;/a&gt; and &lt;a href="http://knowledgehills.com/python/negative-indexing-slicing-stepping-comparing-lists.htm"&gt;Python&lt;/a&gt; provide convenience methods and properties to access and modify them.&lt;/p&gt;

&lt;p&gt;The only way to do this in ES6 is with indices. But even a trivial task like swapping the last elements in two arrays quickly becomes difficult to read.&lt;/p&gt;


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


&lt;p&gt;The most common solution today is to use libraries like &lt;a href="https://lodash.com/"&gt;Lodash&lt;/a&gt; or &lt;a href="https://underscorejs.org/"&gt;Underscore&lt;/a&gt; that include utility functions for accessing the last element. This offers an improvement in clarity, but introduces several complications. Most obviously, they do not include a way to &lt;em&gt;set&lt;/em&gt; the last element. Even if they did, doing so would add ambiguity by removing the &lt;code&gt;=&lt;/code&gt; assignment operator.&lt;/p&gt;


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


&lt;p&gt;That is where the ECMAScript Technical Candidate (TC39) proposal comes in. In its current form, it introduces two new properties: &lt;code&gt;Array.lastItem&lt;/code&gt; and &lt;code&gt;Array.lastIndex&lt;/code&gt;. The first property is especially useful because it works both for setting and getting the last item.&lt;/p&gt;


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


&lt;p&gt;This proposal is actually &lt;a href="https://kangax.github.io/compat-table/esnext/"&gt;already supported&lt;/a&gt; in &lt;a href="https://github.com/zloirock/core-js"&gt;core-js 3&lt;/a&gt; and can be used with Babel and TypeScript today. However, I do not have much use for &lt;code&gt;Array.lastIndex&lt;/code&gt; and would prefer a symmetrical for the first element. For prototypes and smaller projects I wrote two simple, dependency-free properties that do just that.&lt;/p&gt;


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


&lt;p&gt;I recently learned that destructuring in ES6 can &lt;a href="https://medium.com/@frontman/how-swap-two-values-without-temporary-variables-using-javascript-8bb28f96b5f6"&gt;swap array elements without temporary variables&lt;/a&gt;. This is a great example of where &lt;code&gt;Array.first&lt;/code&gt; and &lt;code&gt;Array.last&lt;/code&gt; can really improve clarity.&lt;/p&gt;


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


&lt;p&gt;Suggestions of properties like &lt;code&gt;Array.first&lt;/code&gt; and &lt;code&gt;Array.last&lt;/code&gt; go &lt;a href="https://esdiscuss.org/topic/array-prototype-last"&gt;back to at least 2013&lt;/a&gt;. Hopefully this &lt;a href="https://github.com/tc39/proposals#stage-1"&gt;Stage 1 proposal&lt;/a&gt; continues to get traction and will be coming to a version of JavaScript near you soon.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tc39</category>
      <category>language</category>
    </item>
  </channel>
</rss>
