<?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>Forem: Roxi</title>
    <description>The latest articles on Forem by Roxi (@roxyoanes).</description>
    <link>https://forem.com/roxyoanes</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%2F350284%2Fd846f14a-a394-4613-bec8-7c00de1b48d6.jpeg</url>
      <title>Forem: Roxi</title>
      <link>https://forem.com/roxyoanes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/roxyoanes"/>
    <language>en</language>
    <item>
      <title>A short Explanation of JavaScript Objects</title>
      <dc:creator>Roxi</dc:creator>
      <pubDate>Tue, 17 Mar 2020 16:25:32 +0000</pubDate>
      <link>https://forem.com/roxyoanes/a-short-explanation-of-javascript-objects-bf9</link>
      <guid>https://forem.com/roxyoanes/a-short-explanation-of-javascript-objects-bf9</guid>
      <description>&lt;p&gt;Objects are the general building block upon which much of JavaScript is built. They are one of the six primary types in JS: string, boolean, number, null, undefined and object.&lt;/p&gt;

&lt;p&gt;There are two types of objects. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;literal form - you can add one or more key/value pairs
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;constructed form - you must add the properties one by one
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;myExample&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing objects
&lt;/h2&gt;

&lt;p&gt;The contents of an object consist of values stored at specifically locations, called properties.&lt;/p&gt;

&lt;p&gt;Consider the next example:&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;var&lt;/span&gt; &lt;span class="nx"&gt;myExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;myExample&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;span class="nx"&gt;myExample&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&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;To access the value at the location &lt;code&gt;a&lt;/code&gt; in &lt;code&gt;myExample&lt;/code&gt; we use either "." or "[]". The ".a" syntax is referred to as "property access" and "["a"]" syntax is usually referred to as "key access". In reality the both access the same location, so both terms can be used interchangeably.&lt;/p&gt;

&lt;h2&gt;
  
  
  Property descriptor
&lt;/h2&gt;

&lt;p&gt;All properties are described as property descriptor, because an object property is much more than just its value. It includes three other characteristics:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;writable - the ability to change the value of a property.&lt;/li&gt;
&lt;li&gt;configurable - as long as a property is configurable, we can modify its descriptor definition.&lt;/li&gt;
&lt;li&gt;enumerable - controls whether a property will show up in certain object-property enumerations, such as &lt;code&gt;for..in&lt;/code&gt; loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Immutability
&lt;/h2&gt;

&lt;p&gt;Objects can have their mutability controlled to various levels of immutability. All the approaches that are going to be mentioned create a "shallow immutability". This means that they affect only the object and its direct property characteristics.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Object constant - by combining "writable: false" and "configurable: false", you can create a constant as an object property.&lt;/li&gt;
&lt;li&gt;Prevent extensions - if you want to prevent an object from having new properties, call &lt;code&gt;Object.preventExtensions()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.seal()&lt;/code&gt; - it takes an existing object and calls &lt;code&gt;Object.preventExtensions()&lt;/code&gt; on it and marks all its existing properties as "configurable: false".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.freeze()&lt;/code&gt; - it takes an object and calls &lt;code&gt;Object.seal()&lt;/code&gt; on it and marks the existing properties as "writable: false", so that their values can't be changed; this is the highest level of immutability that can be attained.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>womenintech</category>
    </item>
    <item>
      <title>Notes on this from "this and object prototypes"</title>
      <dc:creator>Roxi</dc:creator>
      <pubDate>Fri, 13 Mar 2020 16:27:57 +0000</pubDate>
      <link>https://forem.com/roxyoanes/notes-on-this-from-this-and-object-prototypes-5g2p</link>
      <guid>https://forem.com/roxyoanes/notes-on-this-from-this-and-object-prototypes-5g2p</guid>
      <description>&lt;p&gt;&lt;code&gt;this&lt;/code&gt; provides an elegant way of implicitly 'passing along' an object reference, leading to cleaner API design and easier reuse.&lt;/p&gt;

&lt;p&gt;There are two meanings often assumed, but both are wrong:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; equals the function itself.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; equals the function's scope.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is actually a binding that is made when a function is invoked. What it references is determined entirely by the call-site where the function is called.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four rules of &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Default Binding
&lt;/h4&gt;

&lt;p&gt;this is the default rule when none of the other rules apply.&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;ball&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;ball&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;h4&gt;
  
  
  Implicit Binding
&lt;/h4&gt;

&lt;p&gt;Here, the call-site uses the &lt;code&gt;obj&lt;/code&gt; context to reference the function.&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;ball&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ball&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ball&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ball&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;h4&gt;
  
  
  Explicit Binding
&lt;/h4&gt;

&lt;p&gt;ball.call() allows us to force its &lt;code&gt;this&lt;/code&gt; to be the &lt;code&gt;obj&lt;/code&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;ball&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;ball&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;h4&gt;
  
  
  new Binding
&lt;/h4&gt;

&lt;p&gt;By calling &lt;code&gt;ball()&lt;/code&gt; with &lt;code&gt;new&lt;/code&gt; in front of it, we have constructed a new object and set that new object as the &lt;code&gt;this&lt;/code&gt; for the call of &lt;code&gt;ball()&lt;/code&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;ball&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&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;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ball&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&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;bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Order of precedence
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Called with &lt;code&gt;new&lt;/code&gt;? Use the newly constructed object.&lt;/li&gt;
&lt;li&gt;Called with &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;apply&lt;/code&gt;? Use the specified object.&lt;/li&gt;
&lt;li&gt;Called with a context object? Use the context object.&lt;/li&gt;
&lt;li&gt;Default binding; if 'strict mode' =&amp;gt; undefined.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Arrow Functions
&lt;/h2&gt;

&lt;p&gt;Arrow functions are signified not by the &lt;code&gt;function&lt;/code&gt; keyword, but by the so-called "fat-arrow" operator =&amp;gt;. Instead of using the four rules, arrow-functions adopt the &lt;code&gt;this&lt;/code&gt; binding from the enclosing scope.&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;ball&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="c1"&gt;//return an arrow-function&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//'this' here is lexically inherited from 'foo()'&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&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;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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;baz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ball&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//2, not 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;the arrow-function created in &lt;code&gt;ball()&lt;/code&gt; captures whatever &lt;code&gt;ball()&lt;/code&gt;s &lt;code&gt;this&lt;/code&gt; is at its call-time. &lt;code&gt;ball()&lt;/code&gt; was &lt;code&gt;this&lt;/code&gt; bound to &lt;code&gt;obj1&lt;/code&gt;, so &lt;code&gt;baz&lt;/code&gt; will be &lt;code&gt;this&lt;/code&gt; bound to &lt;code&gt;obj1&lt;/code&gt; as well.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>womenintech</category>
    </item>
  </channel>
</rss>
