<?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: Brandon LeBoeuf</title>
    <description>The latest articles on DEV Community by Brandon LeBoeuf (@brandonleboeuf).</description>
    <link>https://dev.to/brandonleboeuf</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F460164%2Fa16a448e-767b-46cd-a5b2-c68ad4344a36.png</url>
      <title>DEV Community: Brandon LeBoeuf</title>
      <link>https://dev.to/brandonleboeuf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brandonleboeuf"/>
    <language>en</language>
    <item>
      <title>3DVista and Next.js</title>
      <dc:creator>Brandon LeBoeuf</dc:creator>
      <pubDate>Thu, 30 Dec 2021 22:10:13 +0000</pubDate>
      <link>https://dev.to/brandonleboeuf/3dvista-and-nextjs-1e17</link>
      <guid>https://dev.to/brandonleboeuf/3dvista-and-nextjs-1e17</guid>
      <description>&lt;h1&gt;
  
  
  Use 3DVista in a Next.js app
&lt;/h1&gt;

&lt;p&gt;The default export for &lt;a href="https://www.3dvista.com/en/products/virtualtour"&gt;3DVista's Virtual Tour PRO&lt;/a&gt; is not supported for frameworks like React or Next.js. Below are the steps that need to be taken in order to get 3DVista working on next.js.&lt;/p&gt;

&lt;p&gt;Tested it on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mac: Chrome/Safari/Firefox&lt;/li&gt;
&lt;li&gt;PC: Chrome/Safari/Firefox&lt;/li&gt;
&lt;li&gt;iPhone: Safari/Chrome&lt;/li&gt;
&lt;li&gt;iPad: Safari/Chrome&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Export from 3DVista
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Click &lt;em&gt;Publish&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Select &lt;em&gt;For Web / Mobile&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Set the Destination to a New Folder called &lt;em&gt;3DVista&lt;/em&gt; (this will end up in you next.js projects &lt;em&gt;/public&lt;/em&gt; directory)&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Publish&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Add to next.js project
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add the newly created &lt;em&gt;3DVista&lt;/em&gt; folder into the pages directory of your project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Move files/folders
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Move these files from /pages/3DVista to /public:

&lt;ul&gt;
&lt;li&gt;/lib&lt;/li&gt;
&lt;li&gt;/locale&lt;/li&gt;
&lt;li&gt;/media&lt;/li&gt;
&lt;li&gt;script_general.js&lt;/li&gt;
&lt;li&gt;script.js&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Update CSS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file called &lt;em&gt;3dvista.css&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Remove all declarations in the style tag in /pages/index.htm and place them in &lt;em&gt;3dvista.css&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Remove all inline styles, replacing with a class, and putting these classes into &lt;em&gt;3dvista.css&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;import &lt;em&gt;3dvista.css&lt;/em&gt; file at the top of _&lt;em&gt;app.js&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Edit index.htm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Rename &lt;em&gt;index.htm&lt;/em&gt; to &lt;em&gt;index.jsx&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Remove outer html and head tags and the !DOCTYPE declaration:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  ...
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  ...
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Convert to a functional component
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Head&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/head&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Head&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      ...
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Head&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      ...
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Convert script tag to dangerouslySetHtml:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;tour&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;devicesUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;general&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;script_general.js?v=1640882506117&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;deviceType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;general&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to (*see bonus for alternate strategy here)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;script&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;
  &lt;span class="na"&gt;dangerouslySetInnerHTML&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;__html&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
      let tour;
      let devicesUrl = { general: "script_general.js" };

      (function () {
        let deviceType = ["general"];
      ...
      `&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Be sure to have the __html wrapped in backticks `` to avoid single/double quote conflicts&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Remove this comment which is causing an error: &lt;code&gt;`//Force hide. Some cases the transitionend event isn't dispatched with an iFrame.`&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remove the &lt;code&gt;`isSafariDesktopV11orGreater()`&lt;/code&gt; function, as its causing an error&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Replace all instances of &lt;code&gt;`/Mobile\/\w+/`&lt;/code&gt; with &lt;code&gt;`/Mobile/`&lt;/code&gt;, as they are causing a regex error&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are query parameters on the .txt, .js, and .jpg in the link and script tags that need to be removed. Locate this (looks something like this &lt;code&gt;?v=1640882506117&lt;/code&gt;) at the first link tag within the head tag. Remove All occurrences of this query parameter throughout the document (highlight, right click and select "Change All Occurrences")&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  THATS IT!
&lt;/h1&gt;

&lt;p&gt;Hope this is helpful to someone else out there in the interwebz :)&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>3dvista</category>
      <category>virtual</category>
      <category>tour</category>
    </item>
    <item>
      <title>Closures in JavaScript can... </title>
      <dc:creator>Brandon LeBoeuf</dc:creator>
      <pubDate>Fri, 04 Sep 2020 18:38:25 +0000</pubDate>
      <link>https://dev.to/brandonleboeuf/closure-in-javascript-49n7</link>
      <guid>https://dev.to/brandonleboeuf/closure-in-javascript-49n7</guid>
      <description>&lt;h1&gt;
  
  
  An attempt to understand Closures in JavaScript
&lt;/h1&gt;

&lt;p&gt;I gain more understanding of a topic when I get to talk/write about it... much to my wife's horror as she has zero interest&amp;nbsp;in JavaScript (though she is kind and will still listen)!&lt;/p&gt;

&lt;p&gt;In processing my current understanding of &lt;strong&gt;closures&lt;/strong&gt;, I hope that you find it helpful as well!&lt;/p&gt;

&lt;h3&gt;
  
  
  Global Scope
&lt;/h3&gt;

&lt;p&gt;When variables are declared in the global scope, they are accessible by any part of your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="err"&gt;&amp;nbsp;&lt;/span&gt; 
&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This data is exposed throughout the application... &lt;/p&gt;

&lt;p&gt;You can isolate, or protect, the data from the rest of your application by using closures...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="err"&gt;&amp;nbsp;&lt;/span&gt; 

&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: val is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;val +=1&lt;/code&gt; no longer has access to change &lt;strong&gt;val&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution Context
&lt;/h3&gt;

&lt;p&gt;Within a function, a new &lt;em&gt;execution&amp;nbsp;context&lt;/em&gt; is initialized in which variables can be created, mutated, reassigned, and/or read. Unless something gets&amp;nbsp;returned&amp;nbsp;out of that function, it exists in that functions execution context and in that context alone.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;After completion, everything that was created within the&amp;nbsp;execution&amp;nbsp;context is gone, and the only thing that remains is that which was&amp;nbsp;returned. &lt;/p&gt;

&lt;p&gt;Clousers allow you to isolate and protect data so that it can only be accessed by certain parts of your application, keeping that data isolated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closures
&lt;/h3&gt;

&lt;p&gt;Here, nothing can change the data inside of &lt;strong&gt;assignValue&lt;/strong&gt; due to closures, &lt;strong&gt;{}&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;assignValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;&amp;nbsp;&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;assignValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newVal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="err"&gt;&amp;nbsp;&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;newVal&lt;/strong&gt; is assigned the result of calling the function&amp;nbsp;&lt;strong&gt;assignValue&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Once that execution&amp;nbsp;context is completed, the only thing that remains from the function is that which was returned. Everything inside the&amp;nbsp;execution&amp;nbsp;context is gone, and only what was returned gets stored in &lt;strong&gt;newVal&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The data inside &lt;strong&gt;assignValue&lt;/strong&gt; remains isolated from the rest of our applications.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;br&gt;
&lt;code&gt;val +=1&lt;/code&gt; would still throw a ReferenceError &lt;/p&gt;
&lt;h3&gt;
  
  
  Maintain State
&lt;/h3&gt;

&lt;p&gt;What if we want part of our application to be able to interact with that data?&lt;/p&gt;

&lt;p&gt;Instead of just incrementing a number and returning it, we will create a new function inside the execution context (which still accomplishes the same task as before) but then returns that new &lt;em&gt;function&lt;/em&gt; instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;assignValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;&amp;nbsp;&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;&amp;nbsp;&lt;/span&gt; 
 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
 &lt;span class="p"&gt;};&lt;/span&gt;

 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;assignValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newVal&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newVal&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newVal&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since the function &lt;strong&gt;addOne&lt;/strong&gt;&amp;nbsp;had access to the variables within the original execution context, we can still increment &lt;strong&gt;val&lt;/strong&gt;, even though the initial execution context of &lt;strong&gt;assignValue&lt;/strong&gt;&amp;nbsp;is now gone.&lt;/p&gt;

&lt;p&gt;We are able to maintain state, while still isolation the values inside &lt;strong&gt;assignValue&lt;/strong&gt; from the rest of our application. &lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Closures can allow you to prevent other parts of your application from being able to interact with data.&lt;/p&gt;

&lt;p&gt;You can use Closures to isolate state from the rest of your application.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>closures</category>
      <category>state</category>
    </item>
  </channel>
</rss>
