<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Arjun]]></title><description><![CDATA[My Site]]></description><link>https://arjunsn.now.sh</link><generator>GatsbyJS</generator><lastBuildDate>Wed, 17 Jun 2020 03:31:47 GMT</lastBuildDate><item><title><![CDATA[Short notes on JavaScript.The Core(2)]]></title><description><![CDATA[Object An object is a collection of properties, with a single prototype object(can be an object or null) A prototype is internally…]]></description><link>https://arjunsn.now.sh/js-short-notes/Short-notes-on-JavaScript-The-Core-2/</link><guid isPermaLink="false">https://arjunsn.now.sh/js-short-notes/Short-notes-on-JavaScript-The-Core-2/</guid><pubDate>Sun, 07 Jun 2020 03:12:32 GMT</pubDate><content:encoded>&lt;h2&gt;Object&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;An object is a collection of properties, with a single prototype object(can be an object or null)&lt;/li&gt;
&lt;li&gt;A prototype is internally [[Prototype]] and for  user-level code, it is exposed as &lt;strong&gt;proto&lt;/strong&gt; property of any object&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Prototype&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Object receives prototype when it is created. Defaults to Object.prototype&lt;/li&gt;
&lt;li&gt;Inheritance is achieved via prototype in JS object. If a property is not found the object, its prototype(&lt;strong&gt;proto&lt;/strong&gt;) is searched until the prototype link ends in null or the value is found. Since this process happens at runtime, it is called dynamic dispatch. Also called delegation.&lt;/li&gt;
&lt;li&gt;The inheritance chain is completely mutable.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; protoA &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; protoB &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Same as `let objectC = {__proto__: protoA};`:&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; objectC &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Object&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;protoA&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;objectC&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 10&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Change the delegate:&lt;/span&gt;
Object&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setPrototypeOf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;objectC&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; protoB&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;objectC&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 20&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;PS:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[[prototype]]&lt;/strong&gt; - the actual prototype of an object&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;prototype&lt;/strong&gt; - a property of the constructor function, which references the object which will set a [[prototype]] of the object created using that function&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;strong&gt;proto&lt;/strong&gt;&lt;/strong&gt; - non standard implementation to provide reference to the internal prototype of an object&lt;/p&gt;
&lt;h2&gt;Class&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A class is a formal abstract set which specifies initial state and behavior of its objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Class&lt;/strong&gt; in Javascript is just a syntactic sugar of protoype inheritace. Same behavious with a better syntax.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// General way of using the same object as a prototype of multiple objects**&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; letter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;number&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; __proto__&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; letter&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;number&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; __proto__&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; letter&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; z &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;number&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; __proto__&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; letter&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 1&lt;/span&gt;
  b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 2&lt;/span&gt;
  z&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 26&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can do the same with constructor functions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A constructor function in JS is a normal function which when invoked with &lt;code class=&quot;lang-text&quot;&gt;new&lt;/code&gt; operation will create an object.&lt;/li&gt;
&lt;li&gt;A constructor function, creates an object and sets the &lt;strong&gt;&lt;strong&gt;proto&lt;/strong&gt;&lt;/strong&gt; property of the newly created object to the functions prototype object.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;number &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;prototype&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;getNumber&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; z &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 1&lt;/span&gt;
  b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 2&lt;/span&gt;
  z&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 26&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now the same with &lt;strong&gt;class&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// achieving the same behavior above with class**&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;number &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
  &lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; z &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 1&lt;/span&gt;
  b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 2&lt;/span&gt;
  z&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 26&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;A class can be considered as a combination of prototype object and the constructor function.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you just print the prototype links, you can find that the &lt;code class=&quot;lang-text&quot;&gt;getNumber&lt;/code&gt; function is added to the prototype of Letter and that prototype is assigned to the &lt;code class=&quot;lang-text&quot;&gt;__proto__&lt;/code&gt; of all the newly created object with the class&lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/bff0cdecbe5eeef50ee66904cc4584c2/8f0f9/Screenshot_from_2020-05-19_22-13-40.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 68.24324324324324%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsSAAALEgHS3X78AAABKElEQVQ4y6WTiW7DIBBE+f+PrOpcxRhzHwYf042tJlFURUq80hMY8GjZWVgrWgiikz2+Go7mcAHnHJK++36j6zporde5Ugr8u4E8HCHadt0TQqxYa8HkSYL/0EHp4dyAGBNySpimCcuy3Jnv82odijYYa0V9oJQClmyC6g2Cy8ih4jE2gedxwatg0UVkEzGkjHH6E3z907b//xkW4kDpzigjUTeGHbAD1fDEBVqpYXyixQl5ByyEuDoYyYicM/YG8yHAeY90reE4PhnyPlTDCEP9kyjDq+DKNO8Q1AFRUZbk9OXMYZ0jwc3FjwSro4bUGS0Zo7WlxXnnlT31Ib2OWu/1m+flc0ElNGxn4GzE6Xhe61nH5Zbl2y5XX1DsQI+8h1bm5vSnGf4Co/pLG+mAgbIAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/Screenshot_from_2020-05-19_22-13-40.png&quot;
        title=&quot;&quot;
        src=&quot;/static/bff0cdecbe5eeef50ee66904cc4584c2/fcda8/Screenshot_from_2020-05-19_22-13-40.png&quot;
        srcset=&quot;/static/bff0cdecbe5eeef50ee66904cc4584c2/12f09/Screenshot_from_2020-05-19_22-13-40.png 148w,
/static/bff0cdecbe5eeef50ee66904cc4584c2/e4a3f/Screenshot_from_2020-05-19_22-13-40.png 295w,
/static/bff0cdecbe5eeef50ee66904cc4584c2/fcda8/Screenshot_from_2020-05-19_22-13-40.png 590w,
/static/bff0cdecbe5eeef50ee66904cc4584c2/efc66/Screenshot_from_2020-05-19_22-13-40.png 885w,
/static/bff0cdecbe5eeef50ee66904cc4584c2/8f0f9/Screenshot_from_2020-05-19_22-13-40.png 1045w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;p&gt;To get visual representation, check the links in the image&lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/814630cf99ea6dbfda2d8dce28e724fe/719d0/js-constructor.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 58.10810810810811%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAABcSAAAXEgFnn9JSAAADH0lEQVQoz51ReUjTcRT/0o8y7TYtB12gaHealZDVtkKnHQwPaEV/mDOjwNC88GjQBQaWHbMwimmU09QtlrbbXe63OefMHU43deaRmjYjj7RWry3qv0LowYf34PE+Bw9lsxOnc18mzjG4Z2YKuDTIep5g3RK5diGap7Kjt6Jhxl7sI/MkNnaHijlLT2FD10kYsgzqwdinhYHPXTABQ6CzKsajqHFLPEeEpHJaAJ19e3s650Z4Jvemey4iJFUU+m0lev1TSYTznjbIORVqo5SFG6UvXomri6NjjgV4dkV1bTymfBgeCGzwRN4PTOkgXGdrv/vE5C59GIoI/ITlpYqUoFJVSiBTlRpSKqL5Mf8qkpxE/+VwYGySM+UC6BuZgPGp7zD5DaDTMeQK3E3cUJFzKsJZXwxOwQPoqboJI69LwMm/Cyj+fjCZ9ngb+XTZLnLCw5Aj1JKgfdGUqOUeQmmz5RJXjPNq+YpKub6TLdGaOGKNkeVeLdaIeWGf3lnhg70dnA4zTA3aYOZ9N6C7oizXfWnOjzIFw/UYL4Tbby537YnYuWK+pxQx8gI0Qi5V1VAT367kxxka6xPlvGoaMjtaQW/FoX/CDuOzg4C/lY2SiAfXEVb7+9Dp9OTMzKxb+fkF1/Ly869mZFy+k5JyriA8kuS9A0PeGxHyOuR2G4rQop0IrdiCkB/iiqvOcYSV54VqXqpcL7jIEbJph0mkVR4XLBZLKZPJQCQUglKhAJVKBeUsFviGxfjfO74hXJMWOtN6hTKnzSF+0TNi57QZe2f/GucoheLj6RazucYNcGOqt6dnuttud9lttmGErfSpelQc6RowwkyvAUZNKvhsb4Gv/e2A0iuPKbNrqeqc2nhVZs0JXdqz2GpS1P6VHkKDwSDpczjATQKe7ujthWat1rUpaLO/nPs8uFsnbTcr31iGjGpTFy4ydzQJTAi3ueN0NIBpBAfLmAaE+roxSsyRNR5CpVJJEfD5abLGxlRcrT7fotNdkEgkZw+Tyd6/w2B/sH7ZAozg5Z4VLWKjqKne2tqJW9q6tN3ipgYJ8dABX/Sf9ROD/LFRkluz8AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/js-constructor.png&quot;
        title=&quot;&quot;
        src=&quot;/static/814630cf99ea6dbfda2d8dce28e724fe/fcda8/js-constructor.png&quot;
        srcset=&quot;/static/814630cf99ea6dbfda2d8dce28e724fe/12f09/js-constructor.png 148w,
/static/814630cf99ea6dbfda2d8dce28e724fe/e4a3f/js-constructor.png 295w,
/static/814630cf99ea6dbfda2d8dce28e724fe/fcda8/js-constructor.png 590w,
/static/814630cf99ea6dbfda2d8dce28e724fe/efc66/js-constructor.png 885w,
/static/814630cf99ea6dbfda2d8dce28e724fe/c83ae/js-constructor.png 1180w,
/static/814630cf99ea6dbfda2d8dce28e724fe/719d0/js-constructor.png 1779w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;h2&gt;Execution context&lt;/h2&gt;
&lt;p&gt;An execution context is a specification device that is used to track the runtime evaluation of the code. Usually execution context is maintained in a stack. An execution context stack will always have &lt;strong&gt;global context&lt;/strong&gt; initially, on will other execution contexts will b added.&lt;/p&gt;
&lt;p&gt;Usually, an execution context will be removed once the execution is completed. An exception to this is the generator functions, which will pass the execution in the middle and get removed from the stack. Later when the execution continues, it gets pushed to stack again.&lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a1b7edcc98ca3d9e1ae9d360edec3c54/adef7/execution-stack.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 24.324324324324326%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABcSAAAXEgFnn9JSAAABh0lEQVQY02No35bxv3N3xv+8hT5PGIAgfbZLR92mqP/t29Nfla0O+hfdZROjJqXBW7jM907LtuSvzVuTvuUt9boNVMqRMsUlrW5z1P/Wzak/6rfE/E+Y4DCfYfeJTS/2ndn2fuvhNZdABh66tKPn8pOT/w9d2Pnv4sNj/zfsWx4PFGbcd3bLs0MXd/w/dnXv/71nNz8HijEfu7In7uydw/8PX9z169zdI/837l+5iCEuJoE/LSWdr6a6RgRk4IkrBxuOXtj3Y/exLQ8On9vzdePu1ZGf/j9luHz/9MuLd0/+P3/7+P8Ld06+Bqm98fRC1PXH5/8fPLvr58V7J//vPbFtIQM6CI3x5/CP8OR19XHgiUoO4ZUUE+duaKmSPHZ535E9J7Y+3Hty68PD53efBiplufn8Quy9N1f/A/n/b7+89H/fqe1LGSbM7GKYvnAiQ2NHDUNBWQ4DNuDu7sxo72TDZudkw+7p584WmxTFBQqGHYc2RBw8u/Ph1gPrru47te3+up3LuwBpFcv1/rDyqQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/execution-stack.png&quot;
        title=&quot;&quot;
        src=&quot;/static/a1b7edcc98ca3d9e1ae9d360edec3c54/fcda8/execution-stack.png&quot;
        srcset=&quot;/static/a1b7edcc98ca3d9e1ae9d360edec3c54/12f09/execution-stack.png 148w,
/static/a1b7edcc98ca3d9e1ae9d360edec3c54/e4a3f/execution-stack.png 295w,
/static/a1b7edcc98ca3d9e1ae9d360edec3c54/fcda8/execution-stack.png 590w,
/static/a1b7edcc98ca3d9e1ae9d360edec3c54/efc66/execution-stack.png 885w,
/static/a1b7edcc98ca3d9e1ae9d360edec3c54/c83ae/execution-stack.png 1180w,
/static/a1b7edcc98ca3d9e1ae9d360edec3c54/adef7/execution-stack.png 1716w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;h2&gt;Environment&lt;/h2&gt;
&lt;p&gt;Every execution context has an associated lexical environment. A lexical environment is a place where all the associations between identifiers and their values are stored and made available to the environment. Each environment has options reference to the parent environment. So an environment is a storage of variables, functions, and classes defined in scope.&lt;/p&gt;
&lt;p&gt;To understand with a code&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 150&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this two environments are there, one is &lt;strong&gt;global&lt;/strong&gt; environment and the other one if &lt;strong&gt;foo&lt;/strong&gt; environment. &lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/4f4d6c057fd262ef622c32987d6561d6/e3189/environment-chain.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 83.1081081081081%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAABcSAAAXEgFnn9JSAAAEnUlEQVQ4y6WT/U8TdxzHL3HZM3E/7actZlmyONQ5dBONisxHjA+z+JA5RbfpJAVHrRJUogyEqfEhDLM5H3hSKCyUCqWlBeq1QJ+OXq/Xa49r71qeSqFFhKo8JFDoZ3f4D5jsk7zzed/98Mo7+b4/SOYalUmSqDNnJ6PW3JR27OL2dix7E2qRJD41Zq5R498vLUxC+ElY+80H2zLjZduz4u0HczdgP1xKth2+/J3teMEOXJSTiO2QLMO2iOPbkLMbddYLyRbsyl4ML/nVQf15kqAKRVZ7zkaT5WySjkxdfjVZAH67ad27u6QJtbuyVzpPXNtNSEuOuiXFh13n//6FFt9MJffkJOA7s1bqEY0an2ppJqa1aoLfzslWDTXZjjJTBrR7Uttsny+6cidVAKYdP/Kx3qEiMRaNdbiaJ4zd2kmdvWGqk9ZMtuENk2ambQZ1qEKI2e4C1IgtCHd5wO3rB9o3IPiY8H31xu00AXjggGgpM+jwj84EgBt2xYYnesEfpiE01QekD4OBcRbYYeolYsHpWZzionbKFxW2lWCiJN0zSzH9M7iTg7sPHu0RgJX1le+5+m3OvjEGvMPOGS5MRaleLMqFqKirzzbLBB0x0m99jrjZAOhNdui0kkBzg8D1jwDjH4JuXxB6g2Pw173yQwIwr+jyYj7hwCsYAQH6PBqAgYgXxueHwEjqoHuQAGaQiCGPahTp/yrUmTXypgzeZ1RU1YkflsvO3Ct9LC2vqjudJc35XAAWXM9/S66pPvSktTaLl5hXRq2yMkPRUpOhNtSLVYb6zMeK0pPIm8z96ofI/5olG5C45SmLPhL8Onj9b8XmJW/vlq4qSL2wtvJYwfbSI3mbS9Pyt5bxviwtf1up6Hxi2S7JqhLkt/Wq4nNJaHFuir4kd6e+5OIOQ/G5rc33pUmafyTrNXdEKwpXCMDVSQnvrzvxqXzL6S88mTcPesU39nPp1/exp3iJb6Sy2yRfutf+9IkRES9vtWd9ZSSuH7R7Sn6m2Fs/kt5r+wn6zNdmW/qyFnr3Z0VbBOCxo6c+rG+vMKmwmolGc9VIk1U2KjeUjSotVaOKjopnKqtsvA4t60FqH1pCjVVUWClzhRWPHGF5BR5WVlPhJpk7VFfeFfk9527KwqMUFcYNRnyeKRiDwDgHkblhCL7ww4v5MPhDNIxMD8DAGDuB4DQDeosVUF6klwM2EFyQ4J2sD67dun2c5y0SifbGdzEdvt7n3cAEiTkP3zvCZ47xPkYH7PN8LwH3GiNIp5WaxZ18qZ1cFCM8UVOXO0q4emYJl3/GbKOh+M79fULCi3kXFrMhqjs03cv3zTEXeMGCZ4iEwZccWN2GmC/sAu8QGUGEEr8utnOh0L3BcbA5WdCiJugbisCD8uoDArDoZlGcswdzL1zKEDnHX4twGTGW37zmheQEZ44gT1Rtjxubn9Y0qJ/KGtQ6mULZKlNqUFljM1otb9TKL+X/sUYA5lzOjusZpT1hPqHFrYfwdB/4n7mBYM1gcenBzprAxnS+euNuns+TvtNmUan0XZpAi0npNeBaTtvZyLWZm4TN6iyqniZU7vgPa8ghPJKBZ5kAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/environment-chain.png&quot;
        title=&quot;&quot;
        src=&quot;/static/4f4d6c057fd262ef622c32987d6561d6/fcda8/environment-chain.png&quot;
        srcset=&quot;/static/4f4d6c057fd262ef622c32987d6561d6/12f09/environment-chain.png 148w,
/static/4f4d6c057fd262ef622c32987d6561d6/e4a3f/environment-chain.png 295w,
/static/4f4d6c057fd262ef622c32987d6561d6/fcda8/environment-chain.png 590w,
/static/4f4d6c057fd262ef622c32987d6561d6/efc66/environment-chain.png 885w,
/static/4f4d6c057fd262ef622c32987d6561d6/e3189/environment-chain.png 1035w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;p&gt;Since there is a link between the environment from child to parent, the identifier resolution work similarly to prototype delegation. Which means, when an identifier is not found in the execution environment, it is searched in its parent, and then its parent until found or reach the end(global).&lt;/p&gt;
&lt;h3&gt;Binding Object&lt;/h3&gt;
&lt;p&gt;A global environment has an associated binding object. The biding object may store some properties from the record, but not the others, and vice-versa.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Legacy variables using `var`.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Modern variables using `let`.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Both are added to the environment record:&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 10&lt;/span&gt;
  y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 20&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// But only `x` is added to the &quot;binding object&quot;.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// The binding object of the global environment&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// is the global object, and equals to `this`:&lt;/span&gt;
 
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 10&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// undefined!&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Binding object can store a name which is not&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// added to the environment record, since it&apos;s&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// not a valid identifier:&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;not valid ID&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;not valid ID&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 30&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/21fcc3a9c3e49b1aecec366795e6fbe9/e5ca1/env-binding-object.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 67.56756756756756%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAABcSAAAXEgFnn9JSAAADnUlEQVQ4y52Oa0xTZxjHzxZHsk/L4uKWLdHoPuCiY2Y6I4hs3jbZjPMasgaSOadVFmBZNrauYVOIHxTExTtklHaAtWlLtbWDUkppCwd7PXBOr7Q9vdCWcmphiASkpc/OOZ+7L3uSf/7P/3ne9/e+CHe3ePOpdwWFnPeEhWc+6imsOfBga9XHV9chdO2q3LBrD3cjWnZ+k3H32fXGoz/tHKu6eNDC+W2f+XRjufXIj9vR0nMbjGXcjXpa49uOvV2J1JeMmpq/slsuH7OY+ftRY/WWIYxT2MllgPXXqj9p7KwNN4nq/I2dNf4WMY+8KW8MX5c0hG/ILoab6XxJUONvEtZ56TPUuYaKC0hXqzUuueOYltzBEg9uYVFRi2XxKk/KZ4DDZu3xxAIJsfkABCkXhNNeiMz5wJeYgFDaw/aeKQwis77sc0iBclDGQ0w2G5hxAvAACS4ylA2nUiBVqVoZoLxPXBVMOcE/Q6x64o7cZHIiF6CInCtqy/mTeM4/g+ecESvjK6mVCMj77/+CWBw+cBBBILxReGL3ADk1B33akSsM0O5Dy8mnboj+48tMJvFMeNaTicx6M+6YIxOknBkannH4R5l+iXoRgV6N+GdErtQ4+wZNhAF1EL2qgfG/taYpSe/jHxigzTtyNL4QgAnSDPSvWKehwMxSNIBajkByKcQqC8/goVbyO3KygrOGvsvq8JfH1xQUFLzy2tpXmYz0jyi3acfUSrVBoVDp5YpHOqmCzgq9VaNAiWGFwa59NIA+Vj1xGWSoc1jb1n3rJPJfdbPPiPyvahf0IPf+7EJutwmR9s4epE3QjRyua32J2X34zbXXi+sEZTu+6yjdfOZuaRG3vfR9WoxvPd9RsuX0H3uLKi9/UVQtKC75Xrj3g8qmd/I+su5E88uMv3XiSvEbFbdt22u7HYca5PiBX6X4QVr7eVL8U74MP9QgI45ceujcUdttfZNz1732s/qv8wKvS00ssOX+0L72fiItHHTPiAadlEjnprr0Xkqgwam/hjx070kJNBMUvU90DLiW+TfEtXmBRCDGAu1uspx6vgrJhVWIPF2E6fkMTD/LAJmch9jcMsTnXsBkLA1Ts0vZZQBQ60Z4eYEoTrJAg937eSi9AuPBGbBPJsDijoI/uQie+DwEqCVWeCgFvukFSGcBpGo9Py+w32RngephS7HJ4cPpjCl1KKbSjWFGuxcbMjsxes6K2Zkwn2103B+8J5J8+y8+9VxNNwHB0AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/env-binding-object.png&quot;
        title=&quot;&quot;
        src=&quot;/static/21fcc3a9c3e49b1aecec366795e6fbe9/fcda8/env-binding-object.png&quot;
        srcset=&quot;/static/21fcc3a9c3e49b1aecec366795e6fbe9/12f09/env-binding-object.png 148w,
/static/21fcc3a9c3e49b1aecec366795e6fbe9/e4a3f/env-binding-object.png 295w,
/static/21fcc3a9c3e49b1aecec366795e6fbe9/fcda8/env-binding-object.png 590w,
/static/21fcc3a9c3e49b1aecec366795e6fbe9/efc66/env-binding-object.png 885w,
/static/21fcc3a9c3e49b1aecec366795e6fbe9/c83ae/env-binding-object.png 1180w,
/static/21fcc3a9c3e49b1aecec366795e6fbe9/e5ca1/env-binding-object.png 1332w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;h2&gt;Closure&lt;/h2&gt;
&lt;p&gt;To understand closure this two are necessary&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First-class Function:&lt;/strong&gt;  A function that can participate as normal data: be stored in a variable, passed as an argument, or returned as a value from another function. Such functions have to deal with free variables. A variable that is neither an argument nor a local variable is called a free variable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Static Scrope:&lt;/strong&gt; a language implements static scope, if only by looking at the source code one can determine in which environment a binding is resolved.&lt;/p&gt;
&lt;p&gt;Let us see what is &lt;strong&gt;funarg problem.&lt;/strong&gt; It is an abmiguity that arises when dealing with free vairables as of what is the correct environment for binding. Is it the creation environment or call time environment? This is solved by resolving binding at static scope.&lt;/p&gt;
&lt;p&gt;Let’s see some examples&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;funArg&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;funArg&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 10, not 20!&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Pass `foo` as an argument to `bar`.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;foo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above code is an example of  &lt;strong&gt;downward funarg problem.&lt;/strong&gt; Here the ambiguity arises on whether &lt;code class=&quot;lang-text&quot;&gt;x&lt;/code&gt; should be resolved from the function’s execution scope or the scope from the scope at the creation time? Since JS follow static scope, the vairable is resolved from the outer scope, which is the scope at creation time which has the value of 10 for &lt;code class=&quot;lang-text&quot;&gt;x&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
   
  &lt;span class=&quot;token comment&quot;&gt;// Closure, capturing environment of `foo`.&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
  &lt;span class=&quot;token comment&quot;&gt;// Upward funarg.&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; bar&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Call to `foo` returns `bar` closure.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; bar &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 10, not 20!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above code is an example of &lt;strong&gt;upward funarg problem.&lt;/strong&gt; Here the capturing enviroment outlives the context which creates it. Again, technically it doesn’t differ from the same exact mechanism of capturing the definition environment. Just in this case, hadn’t we have the closure, the activation environment of foo would be destroyed. But we captured it, so it cannot be deallocated, and is preserved — to support static scope semantics.&lt;/p&gt;
&lt;p&gt;Some visual to understand this&lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/da597b1148a5c3a944c15e18e2f644a4/67fe0/closure.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 82.43243243243244%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAABcSAAAXEgFnn9JSAAADdklEQVQ4y62QW0xTdxzHaxUUMCaLMyrxbcl8cNuTiaZuRUyUTBFTa+dhrbWlgeE1UWPiBcL2soc9IZfERQ0tWOkGTG3l5mWldPR2etrS9pReTksLpRbYbESziEf62//80yZo9GGJv+ST7+/3O99++zuHd3HvQ7LpoIVqO+Xyt5wg/c0/OPw/ExbqSoV5ou4bzY+8XFWe275VfElAn78m815qVwXONUv9F1rk9MU2hVdyZdeEULqNwEbTmOfNE5Nz6bGRYs0WP0t5JlkrObE4TidAq7vfiSyFiFXq336tYOZ94E3YF+kkybpjFjaQcrL24MhiPBMAnUFzEgeO2txvjGPOJZPVtWShfFl3IJp109HXE7EU6PoMmvyFvxu6ymPzNIRS4ywz68v64mQW6ZI35mATmRB039ecwkYPHQOHJwSUjwF/eBpCk2kYtbohnsrA0JOxrnygJ2rd/jc7DYGkC6YXwpDIBCH97yTWBUhD74D2LDY+MtnD+sE/Gf2gMfLQaJ0asbiivfeGQo9HHek7Pfpf8oGPLIYvyaA5aSSHwo7AKGOjRxhXxMKYnMMMFR57ekPbfizvLUCs5r5T8eqCzUhLikvW5ncFAMA7f/Y0f8uG0pVoXoMoWkbx+nWflOb6wpvX21bwltcRkaiQ9z+rdPOmorcWsuMqXlWVCPdfCwQl5WW7+dxV1cT3+N+qJZJ1NXK5pE6pFNcqFOJapfKI6rhcrJDJuFlyjCDq0e5wXU0NIT16VICDBDt25LUYwed6QizGgd+JRJ/Jq6u9SpmMUkqlLjlBUGfq68cbL18OcD3aOxVSKVkjk4UkIlHru4FF+cD21lasLc3Nn6tv3ZrRdnZOIuK3NZo40kSXWp3g5hxRnVb7D/J2vC8QXxagaRzocbu/yDx7BrPpNNhsNkjE47Dw/DlEwmGgnE6Mw27PcrOTJAc/GGi32XDgX2bztlAwCFGGeY1+xDKRCIt61u/z4Z7boeevZpJJQF79B18ZXcbPXfpVamYGfF4vBu1hemoK5ufm4OWLF1jnZmeBK3St8b2B3NzU2Igv1XV3f2rQ6xsH+vuv9j94wNHA8UdfX8PgwMBVpE337t5tGB4e/knd0UG8G7iG9zFqWeBGxLeICsQBROWunTsry4XCQzmq9pSV7S/nEAo5PYDmfbUqVQl64xW9PT38/wBC/CHOHqD9XQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/closure.png&quot;
        title=&quot;&quot;
        src=&quot;/static/da597b1148a5c3a944c15e18e2f644a4/fcda8/closure.png&quot;
        srcset=&quot;/static/da597b1148a5c3a944c15e18e2f644a4/12f09/closure.png 148w,
/static/da597b1148a5c3a944c15e18e2f644a4/e4a3f/closure.png 295w,
/static/da597b1148a5c3a944c15e18e2f644a4/fcda8/closure.png 590w,
/static/da597b1148a5c3a944c15e18e2f644a4/efc66/closure.png 885w,
/static/da597b1148a5c3a944c15e18e2f644a4/67fe0/closure.png 1101w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;h2&gt;this&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;this&lt;/strong&gt; is a special object which is dynamically and implicitly passed to the code of a context. &lt;strong&gt;this&lt;/strong&gt; can be considered as an extra parameter, which cannot be mutated. The major use-case is the class-based OOP. Consider the below example&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Point&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
  &lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_x&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
  &lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_y&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; p1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; p2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// Can access `getX`, and `getY` from&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// both instances (they are passed as `this`).&lt;/span&gt;
 
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  p1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 1&lt;/span&gt;
  p2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 3&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When &lt;code class=&quot;lang-text&quot;&gt;getX&lt;/code&gt; function is activated, like any other function, a new enviroment is created to store local variable and arguments passed. In addition to these, function environment record gets &lt;code class=&quot;lang-text&quot;&gt;[[ThisValue]]&lt;/code&gt; available. The value to this is bound based on how the function is called. Since in the console log statement, the function is invoked with p1, the value of &lt;code class=&quot;lang-text&quot;&gt;[[ThisValue]]&lt;/code&gt; is set to &lt;code class=&quot;lang-text&quot;&gt;p1&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; bar &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  foo&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
 
  &lt;span class=&quot;token function&quot;&gt;baz&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// `foo`&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;// global or undefined&lt;/span&gt;
 
  bar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;// bar&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// bar&lt;/span&gt;
 
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// global&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token comment&quot;&gt;// `bar.baz`&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;baz&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// bar&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; savedBaz &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;baz&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;savedBaz&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// global&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;strong&gt;arrow functions&lt;/strong&gt; are special in terms of this value: their this is lexical (static), but not dynamic. I.e. their function environment record does not provide this value, and it’s taken from the parent environment.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Comprehensive notes available at &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/javascript-the-core-2nd-edition/&quot;&gt;http://dmitrysoshnikov.com/ecmascript/javascript-the-core-2nd-edition/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Python Custom Metaclass]]></title><description><![CDATA[As discussed in the metaclasses definition, whenever an object is created for a class, the  method of a metaclass is called which in turn…]]></description><link>https://arjunsn.now.sh/python-custom-metaclass/python-custom-metaclass/</link><guid isPermaLink="false">https://arjunsn.now.sh/python-custom-metaclass/python-custom-metaclass/</guid><pubDate>Sun, 03 May 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;As discussed in the metaclasses definition, whenever an object is created for a class, the &lt;code class=&quot;lang-text&quot;&gt;__call__&lt;/code&gt; method of a metaclass is called which in turn calls the &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; and then &lt;code class=&quot;lang-text&quot;&gt;__init__&lt;/code&gt; method of the class for which the object is created.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
  pass

f &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here when &lt;code class=&quot;lang-text&quot;&gt;f&lt;/code&gt; is created, the metaclass of &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt; which is &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; calls the &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; method in &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt;. But since &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt; doesn’t implement the method &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; , it tries to invoke the method from the &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt;’s ancestor which in this case would be &lt;code class=&quot;lang-text&quot;&gt;object&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With that, if you want to override the object instantiation for any object, just implement &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; in the object as below. &lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foobar&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
    def &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;

    def &lt;span class=&quot;token function&quot;&gt;__new__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cls&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Creating instance&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;__new__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cls&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Foobar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Creating instance&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello World!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the same way, since the &lt;code class=&quot;lang-text&quot;&gt;class&lt;/code&gt; is an object of &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; metaclass, the instantiation of the class can be customized by overriding &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; in the metaclass.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;def &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cls&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
  x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; type&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;__new__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cls&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;val &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x &lt;span class=&quot;token comment&quot;&gt;// Here x is a class object&lt;/span&gt;

type&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__new__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// TypeError: can&apos;t set attributes of built-in/extension type &apos;type&apos; &lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
  pass&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Though the above code makes sense, python won’t allow setting attributes to &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; class directly and even if it does, it will end up an infinite loop. So to overcome this, we need to create a custom metaclass to achieve what we want. Custom metaclasses are user-defined metaclasses which are derived from &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; metaclass. So let’s define a custom metaclass first.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CustomMeta&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;type&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
  def &lt;span class=&quot;token function&quot;&gt;__new__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cls&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bases&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dict&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;__new__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cls&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bases&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dict&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;val &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above metaclass just overrides &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; method and creates the intended class object whenever class code is executed. Now let’s see how to use this custom metaclass.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;metaclass&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;CustomMeta&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
  pass

f &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello World!&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Foo&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello World!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we have defined a class and specified it’s metaclass using the &lt;code class=&quot;lang-text&quot;&gt;metaclass&lt;/code&gt; keyword. Now all the classes with this custom metaclass will have attribute &lt;code class=&quot;lang-text&quot;&gt;val&lt;/code&gt; assigned to it automatically. We can use it as a class factory.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[React Virtual DOM]]></title><description><![CDATA[One of the key features of React is its virtual DOM. Whenever the explanation or benefit of virtual DOM is talked about, is it always said…]]></description><link>https://arjunsn.now.sh/react-virtual-DOM/react-virtual-DOM/</link><guid isPermaLink="false">https://arjunsn.now.sh/react-virtual-DOM/react-virtual-DOM/</guid><pubDate>Sat, 02 May 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;One of the key features of React is its virtual DOM. Whenever the explanation or benefit of virtual DOM is talked about, is it always said that DOM manipulations are an expensive operation and that is where virtual DOM helps in reducing the number of DOM manipulation. &lt;/p&gt;
&lt;p&gt;But that is not exactly true. DOM is fast. Even directly manipulating the DOM with vanilla JS or jQuery if faster since there is no overhead of comparison and reconciliation done by virtual DOM. Adding and removing DOM nodes doesn’t take much more than setting a property on a JavaScript object. But what is actually slow is the rendering the DOM update. Rendering includes recalculating boxes, laying out, and painting the browser. Virtual DOM helps in reducing these renders by batching the DOM updates.&lt;/p&gt;
&lt;p&gt;Virtual DOM is an object representation of nodes, elements, attributes, and contents. So whenever there is an update in data, the below process is for updating the actual DOM.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Whenever anything may have changed, the entire UI will be re-rendered in a Virtual DOM representation.&lt;/li&gt;
&lt;li&gt;The difference between the previous Virtual DOM representation and the new one will be calculated.&lt;/li&gt;
&lt;li&gt;The real DOM will be updated with what has actually changed. This is very much like applying a patch.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Virtual DOM uses a diffing algorithm to find the difference between previous DOM and new virtual DOM and does the reconciliation on the changed part of DOM and update the DOM with changes.&lt;/p&gt;
&lt;p&gt;// Todo add browser rendering details&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Web Server Gateway Interface(WSGI)]]></title><description><![CDATA[WSGI is the specification of Python. WSGI is a specification that describes how a web server communicates with the web application and how…]]></description><link>https://arjunsn.now.sh/Web server-gateway-interface-WSGI/webserver-gateway-interface-WSGI/</link><guid isPermaLink="false">https://arjunsn.now.sh/Web server-gateway-interface-WSGI/webserver-gateway-interface-WSGI/</guid><pubDate>Fri, 01 May 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;WSGI is the specification of Python. WSGI is a specification that describes how a web server communicates with the web application and how web servers can be chained to process a request. WSGI was created to standardise way to execute python code for web applications. &lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/82b1665f7f23bc9aaf9f75fb9615eeb9/3534c/wsgi-interface.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 41.891891891891895%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAAAsSAAALEgHS3X78AAAA+klEQVQY03WQzW6DMBCEef8HC8mhOTQSSCXIEIKNsQPlRwIbGzK12yZV2zmg0bLfMEuQpum7U9d1TdO0bes9Y8xau21blmWEkNIJnnOOt34n6PseG+u6bl/yHkHLsiAiiqK6rimlCJ6m6Xk/wKf+hMvyGsexECLP86qqpJTwSBzH8QGjwEc3QpBdC1Fx7rOxrbVG82EYMEnO55LSoiiEkIxRDB/w7SaPL8dwt3tLEjCYoBFIYwxIGGSdTq/7MDzsD/nlYszyozYunOcZT1/+GfYTeORqpaz9vPHfm3EeYGR9w78VoA8i8RuVUrMTDBjMPYy7fCnl5I12ugPwWMjsGZawJAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/wsgi-interface.png&quot;
        title=&quot;&quot;
        src=&quot;/static/82b1665f7f23bc9aaf9f75fb9615eeb9/fcda8/wsgi-interface.png&quot;
        srcset=&quot;/static/82b1665f7f23bc9aaf9f75fb9615eeb9/12f09/wsgi-interface.png 148w,
/static/82b1665f7f23bc9aaf9f75fb9615eeb9/e4a3f/wsgi-interface.png 295w,
/static/82b1665f7f23bc9aaf9f75fb9615eeb9/fcda8/wsgi-interface.png 590w,
/static/82b1665f7f23bc9aaf9f75fb9615eeb9/3534c/wsgi-interface.png 808w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;p&gt;The advantages of WSGI include flexibility and helps to scale. Any WSGI web application/frameworks can be easily replaced with another. Example a Django app can be replaced with Flask. The same way replacements can happen between WSGI containers. &lt;/p&gt;
&lt;p&gt;The working of WSGI is as follows. All request form client reaches the webserver, for example, NGINX and the web server forwards the request to the WSGI container. WSGI container invokes the callable in the WSGI application. The WSGI application then processes the request and sends back the response to the WSGI container which in turn is sent to the webserver and then back to the client.&lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/7c0278803e0f12cd37404742f766e9f0/d56b5/web-browser-server-wsgi.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 32.43243243243243%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAAsSAAALEgHS3X78AAAA50lEQVQY002QyY6CQBCGef+nQZ+BhAMHPYwSvSgKEbrZmrVZBuaTnhjr8KdTVf9SbQkhsiyTUvZ9v67rMAzpVvSbppnnuW3bUqmqqn5Op+Ph4Hme67q+7zuOY0FjkOd5kiRCykQI3kopFBFC7neraZpYq+saLZBREARv8v1+i+MYHza01tCiKALLsqy24lEUhdGilmUBIVrx62Xb9vnswzQ+4fO52+2DxwN/FDkHPodwQhiGl+uVzFoPqL+d0zRjZm4ex9F8AQin6zo4KGLLCKRD8n9naKRi9UPmZtP55DSJUPmOzY/+Abe/USRRGcwyAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/web-browser-server-wsgi.png&quot;
        title=&quot;&quot;
        src=&quot;/static/7c0278803e0f12cd37404742f766e9f0/fcda8/web-browser-server-wsgi.png&quot;
        srcset=&quot;/static/7c0278803e0f12cd37404742f766e9f0/12f09/web-browser-server-wsgi.png 148w,
/static/7c0278803e0f12cd37404742f766e9f0/e4a3f/web-browser-server-wsgi.png 295w,
/static/7c0278803e0f12cd37404742f766e9f0/fcda8/web-browser-server-wsgi.png 590w,
/static/7c0278803e0f12cd37404742f766e9f0/efc66/web-browser-server-wsgi.png 885w,
/static/7c0278803e0f12cd37404742f766e9f0/c83ae/web-browser-server-wsgi.png 1180w,
/static/7c0278803e0f12cd37404742f766e9f0/d56b5/web-browser-server-wsgi.png 1215w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;h3&gt;WSGI servers&lt;/h3&gt;
&lt;p&gt;Green Unicorn, uWSGI, mod_wsgi, CherryPy&lt;/p&gt;
&lt;p&gt;Refs:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=WqrCnVAkLIo&quot;&gt;https://www.youtube.com/watch?v=WqrCnVAkLIo&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04&quot;&gt;https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.fullstackpython.com/wsgi-servers.html&quot;&gt;https://www.fullstackpython.com/wsgi-servers.html&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Node.js internals]]></title><description><![CDATA[Node.js is a cross-platform Javascript runtime, which executes Javascript code outside of the browser environment. It uses the Chrome V…]]></description><link>https://arjunsn.now.sh/Node-js-internals/Node-js-internals/</link><guid isPermaLink="false">https://arjunsn.now.sh/Node-js-internals/Node-js-internals/</guid><pubDate>Wed, 22 Apr 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;Node.js is a cross-platform Javascript runtime, which executes Javascript code outside of the browser environment. It uses the Chrome V8 engine and libuv. V8 engine compiles Javascript code into the C++ code. libuv is a c++ library that provides asynchronous I/O operation like the file system, networking, etc. Node.js acts as an interface, which helps to access low-level C++ API while writing JS code.&lt;/p&gt;
&lt;p&gt;The Node.js has two parts, the JS part, and the C++ part. The JS part is like a wrapper around the C++ part, to make all the API written in C++ available to the JS code were write. Most of the working/operations of Node.js are in C++. In order to wrap and expose the C++ functions/API, &lt;code class=&quot;lang-text&quot;&gt;process.binding&lt;/code&gt; in use. This function returns the C++ module in the JS code. V8 converts values between JS and C++ code.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;assets/_aToI5oI5HYZLQ172.png&quot; alt=&quot;assets/_aToI5oI5HYZLQ172.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Process.binding&lt;/strong&gt; is a bridge where the JavaScript world connects to the C++ code.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;Array&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;Boolean&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;Exception&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;External&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;False&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;Function&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;Int32&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
using v8&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;String&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

#include &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;uv.h&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;

int main() &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  loop &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uv_default_loop&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  uv_tcp_t server&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;uv_tcp_init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;loop&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;server&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  struct sockaddr_in bind_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uv_ip4_addr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;0.0.0.0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;uv_tcp_bind&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;server&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bind_addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  int r &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uv_listen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;uv_stream_t&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;server&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; on_new_connection&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stderr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Listen error!\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uv_run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;loop&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;UV_RUN_DEFAULT&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This libuv snippet is initializing a new TCP connection. As discussed earlier it can do tasks related to OS.&lt;/p&gt;
&lt;p&gt;This V8 snippet is importing c++ definition of JS objects such as Array, and Boolean. V8 engine translates JS objects into their C++ equivalents.&lt;/p&gt;
&lt;p&gt;Node.js is single-threaded and the event loop of the Node also runs in the same thread. But the libraries(C++) that node uses are multithreaded. As mentioned already node uses &lt;code class=&quot;lang-text&quot;&gt;libuv&lt;/code&gt; internally for all OS related operations. libuv maintains a thread pool of size 4 by default. So if there are four cores in the CPU, four operations can be performed parallelly. The default thread pool can also be configured to have more threads in the pool.&lt;/p&gt;
&lt;p&gt;Ref: &lt;a href=&quot;https://medium.com/better-programming/learn-node-js-under-the-hood-37966a20e127&quot;&gt;https://medium.com/better-programming/learn-node-js-under-the-hood-37966a20e127&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[JS bind]]></title><description><![CDATA[Understand this and bind. The value of this in any function depends on the call point of that function. There many ways of binding this to a…]]></description><link>https://arjunsn.now.sh/JS-bind/JS-bind/</link><guid isPermaLink="false">https://arjunsn.now.sh/JS-bind/JS-bind/</guid><pubDate>Sun, 22 Mar 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;Understand this and bind. The value of this in any function depends on the call point of that function. There many ways of binding this to a function like implicit, explicit, etc. One such way of binding is to use the &lt;code class=&quot;lang-text&quot;&gt;bind&lt;/code&gt; method. &lt;code class=&quot;lang-text&quot;&gt;bind&lt;/code&gt; method is available to all functions from the &lt;code class=&quot;lang-text&quot;&gt;Function&lt;/code&gt; prototype. So let us work on our own implementation of &lt;code class=&quot;lang-text&quot;&gt;bind&lt;/code&gt; to understand how it works and how the value of &lt;code class=&quot;lang-text&quot;&gt;this&lt;/code&gt; is defined in any function. To start with lets look in default bind of window object to this in any functions. &lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;greeting&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;greeting&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello undefined&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Arjun&quot;&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello Arjun&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above code, the console will print &lt;code class=&quot;lang-text&quot;&gt;Hello undefined&lt;/code&gt;, but not throw an error saying &lt;code class=&quot;lang-text&quot;&gt;this&lt;/code&gt; is undefined. This is because &lt;strong&gt;default binding&lt;/strong&gt; has taken place where the global object where the function is declared is bound to &lt;code class=&quot;lang-text&quot;&gt;this&lt;/code&gt;.  Thought the bind of this has happened since there is no property named &lt;code class=&quot;lang-text&quot;&gt;name&lt;/code&gt; available in the global object, it prints &lt;code class=&quot;lang-text&quot;&gt;undefined&lt;/code&gt; in the first call to &lt;code class=&quot;lang-text&quot;&gt;greet&lt;/code&gt; whereas in the second call to &lt;code class=&quot;lang-text&quot;&gt;greet&lt;/code&gt;, the value &lt;code class=&quot;lang-text&quot;&gt;name&lt;/code&gt; is set and it is available in the functions. PS: In strict mode default binding is not allowed.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;person &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Arjun&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;greeting&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;greeting&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above, the function object &lt;code class=&quot;lang-text&quot;&gt;person&lt;/code&gt; is assigned two properties &lt;code class=&quot;lang-text&quot;&gt;name&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;greet&lt;/code&gt;.  When &lt;code class=&quot;lang-text&quot;&gt;greet&lt;/code&gt; is called with the owning object, the call site uses that object context and binds the &lt;code class=&quot;lang-text&quot;&gt;person&lt;/code&gt; object to &lt;code class=&quot;lang-text&quot;&gt;greet&lt;/code&gt; function call. This type of binding is called &lt;strong&gt;implicit binding.&lt;/strong&gt; Implicit binding works on function objects as well. Check the below code.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
greet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;person &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Arjun&quot;&lt;/span&gt;

greet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;printMessage&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;greeting&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;greeting&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;person&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

greet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printMessage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello Arjun&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt; With this knowledge of &lt;code class=&quot;lang-text&quot;&gt;this&lt;/code&gt; binding, we can implement the &lt;code class=&quot;lang-text&quot;&gt;bind&lt;/code&gt; function similar to the one available on all Function objects. Calling bind function of a function returns a new function bound the passed in argument. See the code below.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bindClone&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;args&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;scope&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  val&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token class-name&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;prototype&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bindClone &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bindClone

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;printVal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;printVal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// undefined&lt;/span&gt;

printVal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; printVal&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;bindClone&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;printVal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let us see what &lt;code class=&quot;lang-text&quot;&gt;bindClone&lt;/code&gt; function does. First, it stores &lt;code class=&quot;lang-text&quot;&gt;this&lt;/code&gt; value bound to the function. The value of &lt;code class=&quot;lang-text&quot;&gt;this&lt;/code&gt; depends on the binding methods discussed above. Since &lt;code class=&quot;lang-text&quot;&gt;bindClone&lt;/code&gt; will be called on a function object, this will refer to the function object on with &lt;code class=&quot;lang-text&quot;&gt;bindClone&lt;/code&gt; function is called. Next, a new function is returned which takes a variable number of arguments and calls the actual function with given this bound using &lt;code class=&quot;lang-text&quot;&gt;call&lt;/code&gt; function. Then we are adding this function to the Function prototype so that it will be available to all the function objects. The bind function can be made to receive many arguments alone this scope as below.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bindClone&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;args&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; scope &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; otherParams &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;args&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;scope&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;otherParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Character set and Character encoding]]></title><description><![CDATA[Machines understand 0s and 1s. Character sets are created to convert characters between machine code and human-readable symbols. For example…]]></description><link>https://arjunsn.now.sh/character set-and-character-encoding/character set-and-character-encoding/</link><guid isPermaLink="false">https://arjunsn.now.sh/character set-and-character-encoding/character set-and-character-encoding/</guid><pubDate>Fri, 20 Mar 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;Machines understand 0s and 1s. Character sets are created to convert characters between machine code and human-readable symbols. For example, the character &lt;strong&gt;&lt;code class=&quot;lang-text&quot;&gt;a&lt;/code&gt;&lt;/strong&gt; will be encoded based on the character set when it is stored in memory. And when the character is displayed, a decoder is used to convert the content in the memory into a human-readable symbol.&lt;/p&gt;
&lt;p&gt;ASCII is one of the character set which represents a character in 7bit. Since it uses 7 bits, there can be 2^7, 128 characters represented in ASCII. ASCII contains only English alphabets&lt;/p&gt;
&lt;p&gt;So with ASCII, character &lt;code class=&quot;lang-text&quot;&gt;a&lt;/code&gt; will be stored in its binary representation of  &lt;code class=&quot;lang-text&quot;&gt;01100001&lt;/code&gt; which is 97 in decimal which is the character code of &lt;code class=&quot;lang-text&quot;&gt;a&lt;/code&gt; in ASCII.&lt;/p&gt;
&lt;p&gt;Unicode was created to have a universal code for almost all characters across all languages in the world and some commonly used symbols. Unicode uses 1 to 6 bytes. To encode and decode Unicode characters, different techniques were used like UTF-8, UTF-16, UTF-32, etc.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In UTF-8 each character is encoded into 1 to 4 bytes ( the dominant encoding )&lt;/li&gt;
&lt;li&gt;In UTF16 each character is encoded into 1 to two 16-bit words and&lt;/li&gt;
&lt;li&gt;in UTF-32 every character is encoded as a single 32-bit word.&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Amortized runtime]]></title><description><![CDATA[Usually, we calculate runtime by the number of operations done to complete or get a solution. In amortized it is a bit different where we…]]></description><link>https://arjunsn.now.sh/amortized-runtime/amortized-runtime/</link><guid isPermaLink="false">https://arjunsn.now.sh/amortized-runtime/amortized-runtime/</guid><pubDate>Thu, 20 Feb 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;Usually, we calculate runtime by the number of operations done to complete or get a solution. In amortized it is a bit different where we calculate the time complexity by computing the average run time over a series of operations with varying run times.&lt;/p&gt;
&lt;p&gt;The best example is inserting an element into an unbound ArrayList. In this case, inserting into an ArrayList has two different operations that can happen. One is the insertion and the other is the expansion of array. Insertion is considered as cheap operation and expansion is considered as an expensive operation since insertion takes O(1) time and expansion takes O(n) time to copy current elements to the new, bigger array and O(1) time to insert a new element.&lt;/p&gt;
&lt;p&gt;But in this case, we cannot say that insertion in the worst case is O(n) because that is amortized or happens less frequently than the number of cheap operations.&lt;/p&gt;
&lt;p&gt;So, in this case, we calculate the amortized time by taking the average of the total cost of all operation with the total number of operation&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;total cost of all operations / total number of operations&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So with the above example, to insert a six elements into an array list of size 5 will be&lt;/p&gt;
&lt;p&gt;insert element 1 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 2 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 3 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 4 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 5 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 6 → O(N) + O(1)&lt;/p&gt;
&lt;p&gt;For the sixth element, it takes O(N) cost to copy previous N elements into a bigger array and then insert a new element which costs O(1)&lt;/p&gt;
&lt;p&gt;If we continue this, the next expensive operation of copying will occur after 4 cheap operations since the current size of ArrayList will be 10 as we double array size.  &lt;/p&gt;
&lt;p&gt;insert element 1 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 2 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 3 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 4 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 5 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 6 → O(N) + O(1)&lt;/p&gt;
&lt;p&gt;insert element 7 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 8 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 9 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 10 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 11 → O(N) + O(1)&lt;/p&gt;
&lt;p&gt;After this the will be nine cheap operations before the next expensive operation and this patter continues as 5, 4, 9, 19…&lt;/p&gt;
&lt;p&gt;So now to calculate the amortized run time,&lt;/p&gt;
&lt;p&gt;total cost / total operations → N cheap operations * O(1) cost + 1 expensive operation * O(N) cost / N + 1 total operations&lt;/p&gt;
&lt;p&gt;~ O(N) / N&lt;/p&gt;
&lt;p&gt;~ O(1)&lt;/p&gt;
&lt;p&gt;Now if we don’t double the size of the array every time the array is full but increase its size by a constant size say 3, then the amortized time will be different as bellow.&lt;/p&gt;
&lt;p&gt;insert element 1 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 2 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 3 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 4 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 5 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 6 → O(N) + O(1)&lt;/p&gt;
&lt;p&gt;insert element 7 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 8 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 9 → O(N) + O(1)&lt;/p&gt;
&lt;p&gt;insert element 10 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 11 → O(1)&lt;/p&gt;
&lt;p&gt;insert element 12 → O(N) + O(1)&lt;/p&gt;
&lt;p&gt;if we calculate amortized time for this,&lt;/p&gt;
&lt;p&gt;2 * cheap operation of O(1) + 1 * expensive operation of O(N) / 2 + 1 toal operations&lt;/p&gt;
&lt;p&gt;~ O(N)/3&lt;/p&gt;
&lt;p&gt;~ O(N) which is runtime for non amortized expansion. &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The intuition is when amortized expansion happens, one expensive operation of cost O(N) happens after every N operations of O(1) so, that expensive operation can be dividing equally between each N operations which brings down that expensive operation to O(1) on whole. But that is not the case when it is not amortized. Each cheap operation takes a bigger part of the expensive operation.&lt;/p&gt;
&lt;/blockquote&gt;</content:encoded></item><item><title><![CDATA[Python Metaclasses]]></title><description><![CDATA[Metaclasses in python are way of supporting metaprogramming. Python has built-in metaclasses and also supports creating custom metaclasses…]]></description><link>https://arjunsn.now.sh/python-metaclasses/Python Metaclass type/</link><guid isPermaLink="false">https://arjunsn.now.sh/python-metaclasses/Python Metaclass type/</guid><pubDate>Mon, 20 Jan 2020 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;Metaclasses in python are way of supporting metaprogramming. Python has built-in metaclasses and also supports creating custom metaclasses. Understanding metaclasses in python helps to understand under the hood of classes and objects in python. &lt;/p&gt;
&lt;p&gt;In python, to get the instance of or type of or class of an object, there are two ways → accessing &lt;code class=&quot;lang-text&quot;&gt;__class__&lt;/code&gt;attribute of the object and calling &lt;code class=&quot;lang-text&quot;&gt;type(obj)&lt;/code&gt;function. Both returns the same value in new-style classes. Old-style classes before 3 have different result for those though&lt;/p&gt;
&lt;p&gt;So in python, we can refer to the object’s type and it’s class interchangeably. Everything in python is an object.  If so, then a class should also be an object. Then what is the type if the class object or what is the parent class of class object? We will use &lt;code class=&quot;lang-text&quot;&gt;type()&lt;/code&gt; to find the type of or class of the class object&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
	pass

x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;class &apos;__main__.Foo&apos;&gt;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Foo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;class &apos;type&apos;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As shown in the code above &lt;code class=&quot;lang-text&quot;&gt;type(x)&lt;/code&gt; will print &lt;code class=&quot;lang-text&quot;&gt;&amp;lt;class &amp;#39;__main__.Foo&amp;#39;&amp;gt;&lt;/code&gt;because &lt;code class=&quot;lang-text&quot;&gt;x&lt;/code&gt; is of type &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt; or x is an object of class &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt;. But the &lt;code class=&quot;lang-text&quot;&gt;type(Foo)&lt;/code&gt; prints  &lt;code class=&quot;lang-text&quot;&gt;&amp;lt;class &amp;#39;type&amp;#39;&amp;gt;&lt;/code&gt; which say the &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt; is of type &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt;.  &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt; is an object of &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; class. Not only the user-defined classes, but all the built-in classes will also have the same result which is &lt;code class=&quot;lang-text&quot;&gt;&amp;lt;class &amp;#39;type&amp;#39;&amp;gt;&lt;/code&gt;. And again as mentioned, all classes are instances/objects of &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; class, then &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; class should also be an object of a class which is &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; itself. In the short, the class of or parent class of &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt;class is &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; itself.  And this &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; is called a metaclass. How classes are created with metaclass type? Using the &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; function. The &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; function when passed a single argument, it returns the type of the argument. The same function can also be used to create a class dynamically as below.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre class=&quot;lang-jsx&quot;&gt;&lt;code class=&quot;lang-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
	pass

Bar &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Bar&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Foo&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above code create a new class named &lt;code class=&quot;lang-text&quot;&gt;Bar&lt;/code&gt;, inheriting from base class &lt;code class=&quot;lang-text&quot;&gt;Foo&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Let see what those argument are. The syntax is &lt;code class=&quot;lang-text&quot;&gt;type(&amp;lt;name&amp;gt;, &amp;lt;base classes&amp;gt;, &amp;lt;attibutes&amp;gt;)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;&amp;lt;name&amp;gt;&lt;/code&gt;: the name of the class to be created.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;&amp;lt;base classes&amp;gt;&lt;/code&gt;: a tuple object of all classed to inherit from.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;&amp;lt;attibutes&amp;gt;&lt;/code&gt;: a dictinary object of all the attribute to be added to the created class.&lt;/p&gt;
&lt;h2&gt;Instance(class and object of class) creation flow&lt;/h2&gt;
&lt;p&gt;Knowing the flow of instance creation helps understand metaclasses better.&lt;/p&gt;
&lt;p&gt;Creating a class instance which is basically creating objects from classes can be depicted from the below diagram&lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/deebc15dacb711a1669309d7263567ed/d7398/instance-creation.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 418px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 137.83783783783784%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAcCAYAAABh2p9gAAAACXBIWXMAABibAAAYmwFJdYOUAAAGL0lEQVRIx5VWCVCTRxReQq1ng5VwiIBIIigFFAu1dtqxxWssBWsLHiggggg4kEgEQQ5R0UhUwIQmQYKigvVAC3LEQIEkCIJAEBMgEAgJJHIItE69OGT7/z8IxVarb2bnvX373vfv8Xa/H4C3iJOT04Q9MjKi9ba4mpoaHKohhCAgIAC8VdCAcY2B7d69W5/D4ZinpKSYsVgsMwaDQXR0dJyLjrW3t2MxAoEAvFP6+/u1UX3p0qWNarX6uUajGVapVIMdHR2DPT09o0qlsis2NtZmHAz3TjA+n48FxMfHr+js7HyFzBRKJJKR4uLiUSR5VC6Xj6A+RHc5OzvPR2ORWY9tTTNnI/hLfAr3Z9UJHD/QGBQFLwK5aScwwPCDoWaNjQ1KNPnRo0ewvr4e1tXVIbYGdUGxWFzxmSURj8bSz7IBUF3+GfyfBIYcWlxdU1srkzW3PJRIW6QNTU2yZrm8vKKyCODN8OObDTSO4wmtLMePeksiHLr4VPuiQCO9oqCFukgjFAYu0C0OW6Yn9gR4WwB0kNAZhgAsX4oDFrsIgOCC+GTUT3QKKZYGxYEGsyZm0Mh1NYc91+DL1vNPs/yXLM4jW31aQF2mn7/fRi+fYqOXTbUmVBxzN+9PCl+9Z4Ptiih3e+KTLF9CadwGw2zqFwRki0z5FAvSBOATfjBJ/SAVdkvSuqvO7pj7X8tWB5K/6vXd028PgP2bY/VuQLuAbGk8WXOVESSpIAG2VTJ7lekeBNRXRnfSKiabA/HFscMpzLqxvFsgCI2j06OS09P9lAMDa7P4BRhIOdlgJo9sYTp5E8rCSC3lDKiqZvW2pLrror5753y1Mg5vBs0vnmKABQ3SBNnQIBQ0y6CopRm2jwzDnJrqA9jHT66dPQUQVkeS6oroGGBrmgcGKEyjaJFvKAG7vBwDPM3Lj2EJSyE9P/ePhMI7fUxByeCpvNt+2MeiVyKAlpOAiqu7SC+UV+AzRUbv9SCHeahv4EiI9vgwVqyGFhaWlOionX4R4Zu3hoWu8Dsetyzg2BH05EGFD5jOC15sMgEoTXUlDSvOw2eNnL48f3Nr3m6gk7gHzCkN1DPSxBrp7/h8jt1KI/D1NwBMW60DrA7aAPOi9UAvx32GkYhsZCAMmEcspCy2nCybdHfS6COkbNoz+ph7iQRJbORSRWSYB+zLnW5rYWJnYIC3pnn8tKjrWIzXvu8d7df98KVedloM3nWNLaEj3UebF2iiw9tvZTYBKM/0Ig2rr8FnbRkDaL9zi8eP3R6e9WCsRKZjL8o2L7M+bx/5lln4TW+WjcIHTJuy5JGyUJK8ggk7alhdwszwhRXS6gMNTbVn2JfPXb8vq6XdlVZ55ZbxNivaG05fvpWZfrs0P6m5py3q4m+Za9D8pmNWM3nBU8omlNRamQw7a37pyLlxctU9tRg+7G+C/LrfoWKwAxY3ibpuVeXdbx1UQWFzObwrvwfVsBteE95KQ/MfJK+bPQUQWTLxherq6JDqV1XiaX/rMwWc0qR8lpSezRAn5rNkifnsK7SbSUmMOyltiQUsSRKP8/DsHY7iaAY9FM0X0exmIjdlcsmNF7ZbwGd5cEiZMZSxi2To57p21plwDwLr6N55J0N36h72/A5/0t1uFu2Qp27iEV/d09HeutxIX30fR2vCJkOgLfUHc/jBxEUTgJUsd3xnWXxse3FcDPIYLBGRTReIKGYmIvJCpJkaCyjmJiUUoqkoyMS4DPGV7ptvnMEEM45vm2ly3c+SWLKfSMyhOhhiYAO3vaec2Lf3IYhJYOIiGOdxkWe5uEgGFxeRyMWFJXBxtES2tkU8VyuVe/HjdiqVUeUfgL2A4kLxNOzGvYZSpzsDOHoTB2HuO3kBOqwauwglJdM13t68276+h5Hux6jvsThnkhkPHQgCrLgQwDkeAliRXiA5bCtIPrgd0dumtKOsOABfPscSK2SyrZqeHilCXg9EZWUuqG94eFgLvK+sX78ecNhsLOEUnT6/qbERSiUSiDAgyjOPEaKaAT5EEA4GTCbzdVe7srLyQmtr6yuE9YaEQuGZf3L5BwmNRnttanG5XAeE9O1eO6Kjo98fyMXFBbDZbMymUqn/+stwc3PD7L8B05gBIxMbUJMAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/instance-creation.png&quot;
        title=&quot;&quot;
        src=&quot;/static/deebc15dacb711a1669309d7263567ed/d7398/instance-creation.png&quot;
        srcset=&quot;/static/deebc15dacb711a1669309d7263567ed/12f09/instance-creation.png 148w,
/static/deebc15dacb711a1669309d7263567ed/e4a3f/instance-creation.png 295w,
/static/deebc15dacb711a1669309d7263567ed/d7398/instance-creation.png 418w&quot;
        sizes=&quot;(max-width: 418px) 100vw, 418px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;p&gt;The diagram is more self explantory. The flow is&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When a new object for a class is created, the &lt;code class=&quot;lang-text&quot;&gt;__call__&lt;/code&gt; method of the metaclass is called. In this case, the metaclass will be &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The call method calls the &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; method of the class. This is the method which creates an instance and returns it to the &lt;code class=&quot;lang-text&quot;&gt;__call__&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;Then the &lt;code class=&quot;lang-text&quot;&gt;__init__&lt;/code&gt;method of the class is called with the created instance as the first parameter.&lt;/li&gt;
&lt;li&gt;Then the instance is returned  from the &lt;code class=&quot;lang-text&quot;&gt;__call__&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Not lets look at the creation of instance of metaclass which is creating a class itself.&lt;/p&gt;
&lt;p&gt;
  &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/47c1a7b8113c88d43722fec98d4de572/59415/class-creation.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
  
  &lt;span
    class=&quot;gatsby-resp-image-wrapper&quot;
    style=&quot;position: relative; display: block;  max-width: 575px; margin-left: auto; margin-right: auto;&quot;
  &gt;
    &lt;span
      class=&quot;gatsby-resp-image-background-image&quot;
      style=&quot;padding-bottom: 100.67567567567568%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAABibAAAYmwFJdYOUAAAEYklEQVQ4y6VUfUxTVxS/pa1IV2iFjqLyIdAiUxzOwPYHM9n+ULNky3TLtmQLgrg5CR+tVTZwOrXQom2hBQsRcBui2CJQpJR+AGV0baGUaiKlkbbQUqAgy2IgTmJGlrv3HpTNLBqyneTknvPOub/3O+fcHABeIiQSCTuLi4tfmAMhxK2dYEMikUgCJs7hcFzyer03XC5Xnc/nu2UwGN5HA8vLy0HPXZo1lOJ8+gu4ntwYoClgAB0rCWhZTNB+MhEsLUzh0Ryr1VqFsICLi4vQ6XTCgN10o/FNNN7f1RL089dhACz52jfEVNfXf8Q3PfNk1G7/c2hwaGXS44Uu94SJfbYs/F/J/t4S+pSKw9TmRpO1BYlkXeHfqsnbQR74Lj0UzZOWntl/rUZ4tG9Q865UyP1KlHswWgcAQfdtOqUnPzZkHXB6qOK211zxyFwQR1WcTKRoTu/ZombvomoQVbFe2yI/wQiHuuxQ5MbWyhTa7vGPDhxGrtEgGRDvslLDu9m7KToWIxYBJWKAEyax3D14deblRQejpVF+b7i+9+Gli/UfHz92CGkj7ah7dcpaVhJNW5gYjKX6hqtl3uGaGXgCYEHLtSxc1v5tmB0dvplAwONiYrdv3Yb6nhFDvvu+Fdr6tNBjNa1YlM0M9HvPmVQ60q5NGOCkWSxzD1bPoE/pE8RXfHYYt0aLimhsEjnslfXhiHkf9laLhnolApnlurRRX18ZiQGykyPXAWeHhLK5YfG8PjsskisAIf6GeMrxt+m7Pn8r4o3x8/G0X8wg2C6MpdZnMuJSo8Dr4fsALYEE4jhJgNqZuYlmyouI6CuIS9Cwdq728Km7UfbEfdOP2vPZx76Qp6ftRExygJUvJ+fLivgdCYgZkQZAmCsrK+8dPJ6JxjKYMdh0VewUuq4gYZWhZ1Aid5slU6MLvqhHP/woaROJTpXX1b33B4Sp3TZbvLemplZQVMTVORz7nA4Hw1FZ2cQvKSlcgDD50yMfhKEYfaeYr2ry14by0FDZ4jYI7qntDyT6uTnYNmKF6tFRaJqdgXesw/1qz+SKaswOB7weKLdYBlRuF+xGfP3kBBTfbk5GMXpL0iLV+WsM5+/Xqh6P1S/wW5sPXlZ2iPkdrXx+p4InVHeVl7XJM0VqJb9c2XH5SreSW9YqyxZ0dYiQvCuCrrs8zlk2Nn09h0lXBxguPagp/O3e1SZlIqAig94MKYCEnKRnAIRM7wWhiB0S0NnUVX8FYDlAkhNFV+SlRPdykre3stPxz60dsdGJ53aZiOc7jIQLnSYiTztCPNdmJPA0w8SLnUZimdpC/L7VQKhtuIOVNlXIYhkOHMrASI3DoH/stJ9wEN4KAhuUp+dysXc6cZpzaEzdXWGwj+5B/V/93tX3KxULQa1EAKp5xaCKWwSqSr95od5srEerwn7u8Hqlj5eWILIfn7UrFHHgv0iLTAbm/X6MidlozLTZbE5kVzZLpdJQ8H8k0PuMjAxi4JvJZAJ/ARSAEPpC0FemAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
    &gt;
      &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        style=&quot;width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;&quot;
        alt=&quot;assets/class-creation.png&quot;
        title=&quot;&quot;
        src=&quot;/static/47c1a7b8113c88d43722fec98d4de572/59415/class-creation.png&quot;
        srcset=&quot;/static/47c1a7b8113c88d43722fec98d4de572/12f09/class-creation.png 148w,
/static/47c1a7b8113c88d43722fec98d4de572/e4a3f/class-creation.png 295w,
/static/47c1a7b8113c88d43722fec98d4de572/59415/class-creation.png 575w&quot;
        sizes=&quot;(max-width: 575px) 100vw, 575px&quot;
      /&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  
  &lt;/a&gt;
    &lt;/p&gt;
&lt;p&gt;Let’s see the flow &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Initially, &lt;code class=&quot;lang-text&quot;&gt;__prepare__&lt;/code&gt; method is called which can return a dictionary that will be used as a local namespace in the created class.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;lang-text&quot;&gt;Meta-metaclass&lt;/code&gt;, which is in normal cases the &lt;code class=&quot;lang-text&quot;&gt;type&lt;/code&gt; class &lt;code class=&quot;lang-text&quot;&gt;__call__&lt;/code&gt; method is invoked, which calls &lt;code class=&quot;lang-text&quot;&gt;__new__&lt;/code&gt; method of the metaclass. This is where the class object is created and returned.&lt;/li&gt;
&lt;li&gt;Then the &lt;code class=&quot;lang-text&quot;&gt;__init__&lt;/code&gt; method is invoked with created class a first parameter&lt;/li&gt;
&lt;li&gt;Once &lt;code class=&quot;lang-text&quot;&gt;__init__&lt;/code&gt; returns, the created class is returned&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Java abstract vs interface]]></title><description><![CDATA[The below table gives the base implementation difference between abstract and interface which every search result in google gives. abstract…]]></description><link>https://arjunsn.now.sh/abstractvsinterface/2017-02-02-abstractvsinterface/</link><guid isPermaLink="false">https://arjunsn.now.sh/abstractvsinterface/2017-02-02-abstractvsinterface/</guid><pubDate>Thu, 02 Feb 2017 05:12:32 GMT</pubDate><content:encoded>&lt;p&gt;The below table gives the base implementation difference between abstract and interface which every search result in google gives.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;abstract&lt;/th&gt;
&lt;th&gt;interface&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Abstract class can have abstract and non-abstract methods&lt;/td&gt;
&lt;td&gt;Interface can have only abstract methods. Since Java 8, it can have default and static methods also&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Abstract class doesn’t support multiple inheritance&lt;/td&gt;
&lt;td&gt;Interface supports multiple inheritance&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;abstract class can have final, non-final, static and non-static variables&lt;/td&gt;
&lt;td&gt;interface has only static and final variables&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Here is a useful compilation from various website for the search &lt;strong&gt;when to use interface and when to use abstract&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;abstract&lt;/code&gt; class achieves partial abstraction whereas &lt;code class=&quot;lang-text&quot;&gt;interface&lt;/code&gt; achieves fully abstraction.&lt;/p&gt;
&lt;p&gt;An &lt;code class=&quot;lang-text&quot;&gt;abstract&lt;/code&gt; class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design.&lt;/p&gt;
&lt;p&gt;An &lt;code class=&quot;lang-text&quot;&gt;interface&lt;/code&gt; describes a set of methods that can be called on an object but does not provide concrete implementations for all the methods. Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface. This is true of all subclasses of that class as well. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler&lt;/p&gt;
&lt;p&gt;Use &lt;code class=&quot;lang-text&quot;&gt;abstract&lt;/code&gt; classes and inheritance if you can make the statement “A is a B”. Use interfaces if you can make the statement “A is capable of [doing] as”, or also, abstract for what a class is, interface for what a class can do. We can say a triangle is a polygon but it makes no sense to say a triangle is capable of being a polygon&lt;/p&gt;
&lt;p&gt;Consider using abstract classes if any of these statements apply to your situation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You want to share code among several closely related classes.&lt;/li&gt;
&lt;li&gt;You expect that classes that extend your abstract class have many common methods or fields or require access modifiers other than public (such as protected and private).&lt;/li&gt;
&lt;li&gt;You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Consider using interfaces if any of these statements apply to your situation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.&lt;/li&gt;
&lt;li&gt;You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.&lt;/li&gt;
&lt;li&gt;You want to take advantage of multiple inheritances.&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Multiple AsyncTasks]]></title><description><![CDATA[AsyncTask enables the proper and easy use of the UI thread. This class allows you to perform background operations and publish results on…]]></description><link>https://arjunsn.now.sh/multiple-asynctasks/2017-01-24-multiple-asynctasks/</link><guid isPermaLink="false">https://arjunsn.now.sh/multiple-asynctasks/2017-01-24-multiple-asynctasks/</guid><pubDate>Tue, 24 Jan 2017 05:12:32 GMT</pubDate><content:encoded>&lt;p&gt;AsyncTask enables the proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. This is the definition given for &lt;code class=&quot;lang-text&quot;&gt;AsyncTask&lt;/code&gt; in android documentation. Now let us see how this class works internally.&lt;/p&gt;
&lt;p&gt;AsyncTask uses &lt;code class=&quot;lang-text&quot;&gt;Handler&lt;/code&gt;, &lt;code class=&quot;lang-text&quot;&gt;Thread&lt;/code&gt;, &lt;code class=&quot;lang-text&quot;&gt;Executor&lt;/code&gt;, &lt;code class=&quot;lang-text&quot;&gt;FutureTask&lt;/code&gt;, &lt;code class=&quot;lang-text&quot;&gt;Callable&lt;/code&gt; to achieve its functionality, which is to perform the operation in background and post the status and result of the operation onto the main thread. Whenever a new AsyncTask object is created, a new &lt;code class=&quot;lang-text&quot;&gt;Callable&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;FutureTask&lt;/code&gt; instance are created in the constructor.&lt;/p&gt;
&lt;p&gt;Every AsyncTask created will have a common &lt;code class=&quot;lang-text&quot;&gt;THREAD_POOL_EXECUTOR&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;SERIAL_EXECUTOR&lt;/code&gt; at the process level to execute the tasks in a new thread.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;SERIAL_EXECUTOR&lt;/code&gt; - This is the default executor used in AsyncTask, which executed the AsyncTask in a sequential order, using a &lt;code class=&quot;lang-text&quot;&gt;Queue&lt;/code&gt; implementation. However, &lt;code class=&quot;lang-text&quot;&gt;SERIAL_EXECUTOR&lt;/code&gt; used &lt;code class=&quot;lang-text&quot;&gt;THREAD_POOL_EXECUTOR&lt;/code&gt; at the end to execute the task in a new thread.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;THREAD_POOL_EXECUTOR&lt;/code&gt; - This executor is used when the AsyncTask is executed using &lt;code class=&quot;lang-text&quot;&gt;executeOnExecutor()&lt;/code&gt; method. This executed uses Java’s &lt;code class=&quot;lang-text&quot;&gt;ThreadPoolExecutor&lt;/code&gt; to maintain a pool of threads and execute all the asynctasks using threads from that pool.&lt;/p&gt;
&lt;p&gt;So in order to execute multiple AsyncTask in parallel, we have to use &lt;code class=&quot;lang-text&quot;&gt;THREAD_POOL_EXECUTOR&lt;/code&gt; and execute the AsyncTasks.&lt;/p&gt;
&lt;p&gt;Below is the sample of running multiple AsyncTasks&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Request&lt;/span&gt; req &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; allPendingRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BatchTask&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;executeOnExecutor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;AsyncTask&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;THREAD_POOL_EXECUTOR&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; req&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What the above code does is, for all the parallel requests to be made, it creates a new AsyncTask and instead of executing it using &lt;code class=&quot;lang-text&quot;&gt;SERIAL_EXECUTOR&lt;/code&gt;(by using &lt;code class=&quot;lang-text&quot;&gt;execute()&lt;/code&gt; method), we pass in the &lt;code class=&quot;lang-text&quot;&gt;AsyncTask.THREAD_POOL_EXECUTOR&lt;/code&gt; to be used as executor for executing tasks in parallel.&lt;/p&gt;
&lt;p&gt;This &lt;code class=&quot;lang-text&quot;&gt;AsyncTask.THREAD_POOL_EXECUTOR&lt;/code&gt; will queue up all the task and execute it 5 tasks at a time and when any of running task is completed, the next on from the queue is executed. This is repeated until queue becomes empty. &lt;code class=&quot;lang-text&quot;&gt;AsyncTask.THREAD_POOL_EXECUTOR&lt;/code&gt; cannot queue more that 128 tasks at a time. So if we submit more than that, an exception will be thrown.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Softkeyboard on EditText]]></title><description><![CDATA[I have been facing this problem for a while, where the android softkeyboard hides the  on which we are typing. This happens when  is set to…]]></description><link>https://arjunsn.now.sh/edittext_workaround/2016-08-30-edittext_workaround/</link><guid isPermaLink="false">https://arjunsn.now.sh/edittext_workaround/2016-08-30-edittext_workaround/</guid><pubDate>Tue, 30 Aug 2016 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;I have been facing this problem for a while, where the android softkeyboard hides the &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; on which we are typing. This happens when &lt;code class=&quot;lang-text&quot;&gt;android:windowSoftInputMode=&amp;quot;adjustPan&amp;quot;&lt;/code&gt; is set to the activity in &lt;code class=&quot;lang-text&quot;&gt;AndroidManifest.xml&lt;/code&gt;. This flag &lt;code class=&quot;lang-text&quot;&gt;adjustPan&lt;/code&gt; means that whenever the keyboard is visible, the views in the page should adjust itself by panning upwards so that the &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; in focus lies exactly above the keyboard. But the problem is that the adjust pan work for the first time and when &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; is focused the second time, keyboard hides the &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; if it lower in the screen less than the height of the keyboard. &lt;/p&gt;
&lt;p&gt;The cause of the above problem is that the &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; focus is not cleared when the keyboard hides for the first time. The keyboard is hidden by either clicking the back button or but by clicking the EditorAction button in keyboard. So the below workaround will help you in fixing this problem&lt;/p&gt;
&lt;p&gt;First let me put the entire extended &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; code and we will dig into the code later&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CustomEditText&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EditText&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CustomEditText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Context&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AttributeSet&lt;/span&gt; attrs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; defStyle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; attrs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; defStyle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attrs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CustomEditText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Context&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AttributeSet&lt;/span&gt; attrs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; attrs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attrs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CustomEditText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Context&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;AttributeSet&lt;/span&gt; attrs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  	&lt;span class=&quot;token comment&quot;&gt;//you can add you code here to make use of the attrs&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;handleActionBtnClick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;handleActionBtnClick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;setOnEditorActionListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;OnEditorActionListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;onEditorAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;TextView&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; actionId&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;KeyEvent&lt;/span&gt; event&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;InputMethodManager&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getSystemService&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;INPUT_METHOD_SERVICE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;hideSoftInputFromWindow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getWindowToken&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;clearFocus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;onKeyPreIme&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; keyCode&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;KeyEvent&lt;/span&gt; event&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;keyCode &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;KeyEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;KEYCODE_BACK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token function&quot;&gt;clearFocus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;onKeyPreIme&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;keyCode&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; event&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So now what have we done in &lt;code class=&quot;lang-text&quot;&gt;handleBtnClick()&lt;/code&gt;
Here a listener is added to listen EdittorAction button click in softkeyboard, in which hiding the keyboard and clearing the focus on current &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt;. So now if the user hides the keyboard by pressing EditorAction button, and focuses the &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; again it will pan perfectly and &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt; will show above the keyboard&lt;/p&gt;
&lt;p&gt;Next thing to solve is backbutton press. So to do that we are listening for keypress by overriding &lt;code class=&quot;lang-text&quot;&gt;onKeyPreIme()&lt;/code&gt; which is called when back button is pressed with keyboard showing. When &lt;code class=&quot;lang-text&quot;&gt;onKeyPreIme()&lt;/code&gt; is pressed we do a check for back button code &lt;code class=&quot;lang-text&quot;&gt;KeyEvent.KEYCODE_BACK&lt;/code&gt; and clear the focus on the &lt;code class=&quot;lang-text&quot;&gt;EditText&lt;/code&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Multi-Touch in android]]></title><description><![CDATA[Multi-Touch in android was available since Android 2.0. When more than one finger touches the screen multi-touch gesture happens and android…]]></description><link>https://arjunsn.now.sh/multitouch-android/2016-07-20-multi-touch-android/</link><guid isPermaLink="false">https://arjunsn.now.sh/multitouch-android/2016-07-20-multi-touch-android/</guid><pubDate>Wed, 20 Jul 2016 03:12:32 GMT</pubDate><content:encoded>&lt;p&gt;Multi-Touch in android was available since Android 2.0. When more than one finger touches the screen multi-touch gesture happens and android provide various apis to handle these gestures. Lets start with the basic event handling for single finger on the screen&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;onTouchEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt; ev&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; action &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ev&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;action&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
     &lt;span class=&quot;token class-name&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Event handled &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Action finger down&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

   &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_UP&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
     &lt;span class=&quot;token class-name&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Event handled &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Action finger up&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
     &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
   &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above is the one of many methods of handling touches which logs touch down and up of single finger. This work fine as long as we don’t need to handle multiple fingers. But in many cases handling single finger events in not sufficient. For handling multi-touch we need to handle the below two additional events.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;ACTION_POINTER_DOWN&lt;/code&gt; - MotionEvent.ACTION_POINTER_DOWN is the event triggered whenever the secondary pointer touches the screen. If there is already a pointer(finger) on the screen and one more pointer goes in contact with the screen, ACTION_POINTER_DOWN event is triggered. For all the next coming pointers the ACTION_POINTER_DOWN is triggered. MotionEvent.ACTION_DOWN will be triggered only for the first pointer coming in contact with the screen.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;ACTION_POINTER_UP&lt;/code&gt; - MotionEvent.ACTION_POINTER_UP is the event triggered when a pointer goes up and there in yet another pointer still in contact with the screen. The same event is triggered for all pointer going up till the last pointer and MotionEvent.ACTION_UP will be triggered for the last pointer leaving the screen.&lt;/p&gt;
&lt;p&gt;The next two things we should know to keep track of each pointer is are &lt;code class=&quot;lang-text&quot;&gt;Pointer Index&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;Pointer ID&lt;/code&gt;. Lets see what they are and how they help us in handling multi-touch.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;Index&lt;/code&gt; - In a &lt;code class=&quot;lang-text&quot;&gt;MotionEvent&lt;/code&gt; object, the pointer details are stored in array. So, the &lt;code class=&quot;lang-text&quot;&gt;Index&lt;/code&gt; of a pointer is its position in that array. From this we can infer that pointer index in not persistent across the event. To put that with an example, when a first pointer touches the screen, it will be the first object in array, with pointer index 0. When second pointer comes, it will be stored in index 1. Now when the pointer with index 0 goes up, the position of pointer in array is rearranged, which puts the second pointer which is currently stays on screen in index 0.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;ID&lt;/code&gt; - While the pointer index is non persistent, to identify the pointers across events of an entire gesture, &lt;code class=&quot;lang-text&quot;&gt;Pointer ID&lt;/code&gt; comes in handy. &lt;code class=&quot;lang-text&quot;&gt;Pointer ID&lt;/code&gt; is unique ID mapping to the pointer which stays persistent always. So in the example discussed for &lt;code class=&quot;lang-text&quot;&gt;Index&lt;/code&gt;, even when the position of pointer in array changes, its pointer id remains same. That makes the difference between &lt;code class=&quot;lang-text&quot;&gt;Index&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;ID&lt;/code&gt;.    &lt;/p&gt;
&lt;p&gt;Lets find how we can use the above discussed things and handle multi-touch. We will take a real example and make things more clear. Consiter we have three buttons on screen namely &lt;code class=&quot;lang-text&quot;&gt;LEFT&lt;/code&gt; &lt;code class=&quot;lang-text&quot;&gt;RIGHT&lt;/code&gt; &lt;code class=&quot;lang-text&quot;&gt;JUMP&lt;/code&gt;. As the name of the buttons suggests, they perform move right, move left and jump actions respectively. Take a look at the screen below.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://i38.photobucket.com/albums/e125/arjunsna/blogs/screen_zpstrcgxtp4.jpg&quot; alt=&quot;demo screen&quot;&gt;&lt;/p&gt;
&lt;p&gt;Basically how we handle this is when pointer goes down, take that event’s &lt;code class=&quot;lang-text&quot;&gt;X&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;Y&lt;/code&gt; and check whether the point (X,Y) lies with in the area of any of the three button and perform operations accordingly as below&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;onTouchEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; action &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ev&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_MASK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
     &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;startJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

   &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_UP&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
        x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;stopJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// right, left, jump in the above code are&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;//RECT objects which represent the button location on screen.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above code will work for single pointer events. When multiple pointer touches the screen, events will be missed desired output is not achieved. Now will revise the above code so that we will handle all events when multiple pointers touches the screen.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; action &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ev&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_MASK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;startJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

 &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_UP&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;stopJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
       x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;token function&quot;&gt;stopJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
       &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

   &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
        x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;stopJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

 &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now it looks fine, when secondary pointer goes in contact or leaves the screen, &lt;code class=&quot;lang-text&quot;&gt;ACTION_POINTER_DOWN&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;ACTION_POINTER_DOWN&lt;/code&gt; events are triggered respectively and we have handled it well. But this is not enough. If you have noticed, we always use &lt;code class=&quot;lang-text&quot;&gt;motionEvent.getX()&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;motionEvent.getY()&lt;/code&gt; to get x and y co-ordinated. By default &lt;code class=&quot;lang-text&quot;&gt;getX()&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;getY()&lt;/code&gt; returns co-ordinated of pointer in index 0. So what we are doing is always handle co-ordinates of primary poitner even though we handle secondary pointer events. Lets tweek the code above a little more to make it work perfectly.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; action &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ev&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_MASK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;startJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

 &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_UP&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token function&quot;&gt;stopJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; pointerIndexDown &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;
          &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_MASK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_SHIFT&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexDown&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexDown&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;token function&quot;&gt;stopJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
       &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
       &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

   &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_DOWN&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
       &lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; pointerIndexUp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_MASK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_SHIFT&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexUp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexUp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingRight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;setIsPressingLeft&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jump&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;stopJump&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

 &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now lets discuss the changes done above. For events &lt;code class=&quot;lang-text&quot;&gt;ACTION_DOWN&lt;/code&gt; and &lt;code class=&quot;lang-text&quot;&gt;ACTION_UP&lt;/code&gt;, we have not changed anything since it will be triggered on for primary pointer(first pointer which triggers event). For other two event we are finding the index of the pointer for which the event has occurred and get the x,y co-ordinates of the event with the pointer index as below&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; pointerIndexDown &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;
                         &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_MASK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_SHIFT&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; pointerIndexUp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;
                        &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_MASK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MotionEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ACTION_POINTER_INDEX_SHIFT&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above &lt;code class=&quot;lang-text&quot;&gt;pointerIndexDown&lt;/code&gt; will hold the index of pointer which came in contact with screen when there is other pointer(s) already in contact with screen and by which the event was triggered. In other words, it is the recent pointer which came in contact with screen after the primary pointer. And &lt;code class=&quot;lang-text&quot;&gt;pointerIndexUp&lt;/code&gt; holds the index of the pointer which left the screen recently when other pointer(s) is already on the screen. We use these pointer indices to get the correct co-ordinates of the event and perform operation&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;lang-java&quot;&gt;&lt;code class=&quot;lang-java&quot;&gt;x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexDown&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexDown&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexUp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; motionEvent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointerIndexUp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;lang-text&quot;&gt;motionEvent.getX(pointerIndex)&lt;/code&gt; gives the x co-ordinate of event which was triggered by the pointer with given pointer index. Same goes for &lt;code class=&quot;lang-text&quot;&gt;motionEvent.getY(pointerIndex)&lt;/code&gt; which gives y co-ordinate.&lt;/p&gt;
&lt;p&gt;We have discussed one of the many use cases of multi-touch handling. To find a working example of multi-touch, take a look at the following project
&lt;a href=&quot;https://github.com/Arjun-sna/SwipeCards&quot;&gt;https://github.com/Arjun-sna/SwipeCards&lt;/a&gt;&lt;/p&gt;</content:encoded></item></channel></rss>