<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>javascript &amp;mdash; StealthyCoder</title>
    <link>https://stealthycoder.writeas.com/tag:javascript</link>
    <description>Making code ninjas out of everyone</description>
    <pubDate>Wed, 29 Apr 2026 02:49:35 +0000</pubDate>
    <item>
      <title>Ditto is real</title>
      <link>https://stealthycoder.writeas.com/ditto-is-real?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[Anyone who grew up watching the Pokémon anime in the 90s will think are we that old already and secondly about how Ditto plays a role in there. Ditto is a very cool Pokémon who could assume the form of any of the other Pokémon including moves. !--more--&#xA;&#xA;Mocking&#xA;&#xA;So I needed a Ditto to mock some object in a frontend application. The reason for this was I just wanted to mock the entire library and so whatever would be called in whatever order would also return a Ditto as to make it possible to chain ridiculous things like Ditto.a.b.c.d().f and it would work and return a Ditto.&#xA;&#xA;The way you do this is to use a Proxy object in JavaScript. What it does is you provide a target and a handler which means you can intercept and augment some behaviour. For example a simple audit log as to who touched what objects, or maybe click behaviour or even as a form of Synthetic Monitoring. You could also make objects immutable with this behaviour as it would be when you freeze or seal objects. &#xA;&#xA;Target&#xA;&#xA;The target is whatever object you want to add the behaviour to. It can be a function or an object. &#xA;&#xA;Handler&#xA;&#xA;The handler holds the behaviour you want to want the target to obtain. So on an object you can specify a get key with a function that will be executed on getting an object. You can also set an apply key which will be executed on calling_ a function. There are several others you can call, even adding behaviour to when new gets used.&#xA;&#xA;Ditto was born&#xA;&#xA;The following piece of code creates a Ditto.&#xA;&#xA;const Ditto = new Proxy(() =  {}, {&#xA;    get: () =  {return Ditto;},&#xA;    set: () =  {},&#xA;    apply: () =  {return Ditto;},&#xA;});&#xA;&#xA;What it will do is on getting a property you will return the Ditto. On setting a property nothing will happen. So it will not be stored. Then when you execute the Ditto on any property it will return a Ditto. &#xA;&#xA;#code #javascript]]&gt;</description>
      <content:encoded><![CDATA[<p>Anyone who grew up watching the Pokémon anime in the 90s will think are we that old already and secondly about how Ditto plays a role in there. Ditto is a very cool Pokémon who could assume the form of any of the other Pokémon including moves. </p>

<h2 id="mocking" id="mocking">Mocking</h2>

<p>So I needed a Ditto to mock some object in a frontend application. The reason for this was I just wanted to mock the entire library and so whatever would be called in whatever order would also return a Ditto as to make it possible to chain ridiculous things like <code>Ditto.a.b.c.d().f</code> and it would work and return a Ditto.</p>

<p>The way you do this is to use a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy" rel="nofollow"><code>Proxy</code></a> object in JavaScript. What it does is you provide a <code>target</code> and a <code>handler</code> which means you can intercept and augment some behaviour. For example a simple audit log as to who touched what objects, or maybe click behaviour or even as a form of Synthetic Monitoring. You could also make objects immutable with this behaviour as it would be when you <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze" rel="nofollow">freeze</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal" rel="nofollow">seal</a> objects.</p>

<h3 id="target" id="target">Target</h3>

<p>The target is whatever object you want to add the behaviour to. It can be a function or an object.</p>

<h3 id="handler" id="handler">Handler</h3>

<p>The handler holds the behaviour you want to want the target to obtain. So on an object you can specify a <code>get</code> key with a function that will be executed on <em>getting</em> an object. You can also set an <code>apply</code> key which will be executed on <em>calling</em> a function. There are several others you can call, even adding behaviour to when <code>new</code> gets used.</p>

<h2 id="ditto-was-born" id="ditto-was-born">Ditto was born</h2>

<p>The following piece of code creates a Ditto.</p>

<pre><code class="language-JavaScript">const Ditto = new Proxy(() =&gt; {}, {
    get: () =&gt; {return Ditto;},
    set: () =&gt; {},
    apply: () =&gt; {return Ditto;},
});
</code></pre>

<p>What it will do is on getting a property you will return the Ditto. On setting a property nothing will happen. So it will not be stored. Then when you execute the Ditto on any property it will return a Ditto.</p>

<p><a href="https://stealthycoder.writeas.com/tag:code" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">code</span></a> <a href="https://stealthycoder.writeas.com/tag:javascript" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">javascript</span></a></p>
]]></content:encoded>
      <guid>https://stealthycoder.writeas.com/ditto-is-real</guid>
      <pubDate>Wed, 20 May 2020 05:35:34 +0000</pubDate>
    </item>
    <item>
      <title>Powerful concept: FunctionMap</title>
      <link>https://stealthycoder.writeas.com/powerful-concept-functionmap?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[This idea comes from JavaScript when there were no classes yet as there are now in ES6. You would write something like this: !--more--&#xA;&#xA;var greetings = {&#xA; hello: function() { console.info(&#34;Hello&#34;); }&#xA;};&#xA;greetings.hello();&#xA;greetings&#34;hello&#34;;&#xA;This is how functions could be shipped around. So the idea is simple, especially the second one. I started to wonder if we can have a Map of String -  Function in other languages like Java, Python, PHP and more?&#xA;&#xA;Java&#xA;&#xA;We sure can. I was working on a JSON-RPC project where you don&#39;t send entity bodies but you send what method to invoke and with what arguments. In the backend we made it so internally a consistent object was shipped around (RPCServiceResult) and we had Enum for the methods that were allowed. So I set up an EnumMap of Enum -  Function. This made it so the entrypoint of each service was the same. See if the method key exists, if so execute the corresponding function with the given input otherwise throw a MethodNotFound error. This meant we could extend the Enum and EnumMap and the Function mapping but never had to change how the services called each other. &#xA;&#xA;Python&#xA;&#xA;In Python I was working on a silly hacking simulation game in the Console. I wanted to have a list of commands and execute the corresponding function that was that command in game terms. At this point I just came from PHP and there it is completely normal to execute the string itself. I will come back to this in a bit. This is more difficult to do in Python and so I came up with a dict where command name was the key and function references were the values. This made it super simple to maintain. &#xA;&#xA;PHP&#xA;&#xA;So in PHP this is normal:&#xA;function a(){ echo &#34;A&#34;; };&#xA;$a = &#34;a&#34;;&#xA;$a();&#xA;However it is really difficult to maintain and see what is going on, let alone insecure to do this if the input to $a is user input. The following makes more sense and is easier to maintain.&#xA;&#xA;function a() { echo &#34;A&#34;; };&#xA;$commands = [&#34;a&#34; =  function() { calluserfunc(&#34;a&#34;); }];&#xA;if ( arraykeyexists(&#34;a&#34;, $commands) ) { $commands&#34;a&#34;; }&#xA;&#xA;Granted this is a contrived example but I hope it gets the point across. &#xA;&#xA;Scala&#xA;&#xA;I was working on the Advent of Code 2020 and one of the puzzles had a lot of validation rules. Immediately the structure came to mind of having a Map of Functions, for each field to validate the corresponding function. This worked like a charm and it is very easy to see what each rule does. &#xA;&#xA;Java&#xA;&#xA;Another example was that in a particular flow there was a need to either create a User of type A or type B and then continue the flow. It was first like the following:&#xA;if ( type == &#34;UserA&#34; ) {&#xA;    return new UserA();&#xA;} else {&#xA;    return new UserB();&#xA;}&#xA;Now never mind the bug of always returning type B, I suggested the following:&#xA;MapString, Supplier&lt;User  userSupplier = new HashMap(){{ put(&#34;UserA&#34;, UserA::new); put(&#34;UserB&#34;, UserB::new); }};&#xA;Now you could replace it with one entrypoint:&#xA;return userSupplier.getOrDefault(type, UserB::new).get();&#xA;You still have the bug, but it became a feature. So we leave it in.&#xA;The colleague immediately said, this happens in way more places in the code and I can change all of them. &#xA;&#xA;The nice thing again is, we can extend the map to support more types but it does not change the logic of calling the service.&#xA;&#xA;Conclusion&#xA;&#xA;I hope this concept proves useful to you, I find it quite powerful and easy to construct and it gives lots of nice patterns. &#xA;&#xA;#code #javascript #java #python]]&gt;</description>
      <content:encoded><![CDATA[<p>This idea comes from JavaScript when there were no classes yet as there are now in ES6. You would write something like this: </p>

<pre><code class="language-javascript">var greetings = {
 hello: function() { console.info(&#34;Hello&#34;); }
};
greetings.hello();
greetings[&#34;hello&#34;]();
</code></pre>

<p>This is how functions could be shipped around. So the idea is simple, especially the second one. I started to wonder if we can have a <code>Map</code> of <code>String -&gt; Function</code> in other languages like Java, Python, PHP and more?</p>

<h1 id="java" id="java">Java</h1>

<p>We sure can. I was working on a JSON-RPC project where you don&#39;t send entity bodies but you send what method to invoke and with what arguments. In the backend we made it so internally a consistent object was shipped around (<code>RPCServiceResult</code>) and we had <code>Enum</code> for the methods that were allowed. So I set up an <code>EnumMap</code> of <code>Enum -&gt; Function</code>. This made it so the entrypoint of each service was the same. See if the method key exists, if so execute the corresponding function with the given input otherwise throw a <code>MethodNotFound</code> error. This meant we could extend the Enum and EnumMap and the Function mapping but never had to change how the services called each other.</p>

<h1 id="python" id="python">Python</h1>

<p>In Python I was working on a silly hacking simulation game in the Console. I wanted to have a list of commands and execute the corresponding function that was that command in game terms. At this point I just came from PHP and there it is completely normal to execute the string itself. I will come back to this in a bit. This is more difficult to do in Python and so I came up with a dict where command name was the key and function references were the values. This made it super simple to maintain.</p>

<h1 id="php" id="php">PHP</h1>

<p>So in PHP this is normal:</p>

<pre><code class="language-php">function a(){ echo &#34;A&#34;; };
$a = &#34;a&#34;;
$a();
</code></pre>

<p>However it is really difficult to maintain and see what is going on, let alone insecure to do this if the input to <code>$a</code> is user input. The following makes more sense and is easier to maintain.</p>

<pre><code class="language-php">function a() { echo &#34;A&#34;; };
$commands = [&#34;a&#34; =&gt; function() { call_user_func(&#34;a&#34;); }];
if ( array_key_exists(&#34;a&#34;, $commands) ) { $commands[&#34;a&#34;](); }
</code></pre>

<p>Granted this is a contrived example but I hope it gets the point across.</p>

<h1 id="scala" id="scala">Scala</h1>

<p>I was working on the Advent of Code 2020 and one of the puzzles had a lot of validation rules. Immediately the structure came to mind of having a Map of Functions, for each field to validate the corresponding function. This worked like a charm and it is very easy to see what each rule does.</p>

<h1 id="java-1" id="java-1">Java</h1>

<p>Another example was that in a particular flow there was a need to either create a User of type A or type B and then continue the flow. It was first like the following:</p>

<pre><code class="language-java">if ( type == &#34;UserA&#34; ) {
    return new UserA();
} else {
    return new UserB();
}
</code></pre>

<p>Now never mind the bug of always returning type B, I suggested the following:</p>

<pre><code class="language-java">Map&lt;String, Supplier&lt;User&gt;&gt; userSupplier = new HashMap&lt;&gt;(){{ put(&#34;UserA&#34;, UserA::new); put(&#34;UserB&#34;, UserB::new); }};
</code></pre>

<p>Now you could replace it with one entrypoint:</p>

<pre><code class="language-java">return userSupplier.getOrDefault(type, UserB::new).get();
</code></pre>

<p>You still have the bug, but it became a feature. So we leave it in.
The colleague immediately said, this happens in way more places in the code and I can change all of them.</p>

<p>The nice thing again is, we can extend the map to support more types but it does not change the logic of calling the service.</p>

<h1 id="conclusion" id="conclusion">Conclusion</h1>

<p>I hope this concept proves useful to you, I find it quite powerful and easy to construct and it gives lots of nice patterns.</p>

<p><a href="https://stealthycoder.writeas.com/tag:code" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">code</span></a> <a href="https://stealthycoder.writeas.com/tag:javascript" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">javascript</span></a> <a href="https://stealthycoder.writeas.com/tag:java" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">java</span></a> <a href="https://stealthycoder.writeas.com/tag:python" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">python</span></a></p>
]]></content:encoded>
      <guid>https://stealthycoder.writeas.com/powerful-concept-functionmap</guid>
      <pubDate>Mon, 07 Dec 2020 23:10:38 +0000</pubDate>
    </item>
    <item>
      <title>Frontend dev is best dev</title>
      <link>https://stealthycoder.writeas.com/frontend-dev-is-best-dev?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[So this has been the case for many a year now, that everything in JavaScript land or in this day and age, TypeScript land, is targeting the fact that the developer can use one codebase for everything frontend and backend. !--more-- This means you would use the same file for the definitions of the data models that both the NodeJS/Deno backend will use as well as the frontend to query/reason about those objects. Use a simple REST API to ship data back and forth and so on.&#xA;&#xA;I just have one thought/question; why?&#xA;&#xA;Why indeed&#xA;&#xA;Well it seems like more and more was pulled into the frontend as time passed on, mainly to free up the backend to do the real processing. Like number crunching, data processing and so on. Which makes sense, but now it is weird to say the state of an application should be as close to the user as possible. Also servers are now more than capable of doing both work, and therefore the natural conclusion is that since most of the things were in the frontend anyway now; why not move the whole thing to the frontend?&#xA;&#xA;The thing is that it makes the codebase more difficult to manage, you introduce unwanted complexity let alone making it secure. The whole codebase has so many extra dependencies and vulnerabilities since a lot of Server Side Template Injection (SSTI) plague the landscape now and have been fixed/found already in the old backend languages. &#xA;&#xA;Of course it seems convenient, but let&#39;s take the data models that you want to use. You define them either from the frontend perspective or backend perspective first. In the frontend you might only want first name and an e-mail address, for example. Then in the backend you want to have maybe something more than just first name and an email address. How about a password to login? Or a session id, or maybe just an id in general for the databases so you can store multiple combinations of people? Then you have to transform schemas or build them differently. So you get views or something equivalent and suddenly everything is back to this again.&#xA;&#xA;I would steer away from having the whole application in one language with one library/framework since nothing so far checks all the boxes and covers all the bases. &#xA;&#xA;#100DaysToOffload #frontend #JavaScript #devlife&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>So this has been the case for many a year now, that everything in JavaScript land or in this day and age, TypeScript land, is targeting the fact that the developer can use one codebase for everything frontend and backend.  This means you would use the same file for the definitions of the data models that both the NodeJS/Deno backend will use as well as the frontend to query/reason about those objects. Use a simple REST API to ship data back and forth and so on.</p>

<p>I just have one thought/question; why?</p>

<h2 id="why-indeed" id="why-indeed">Why indeed</h2>

<p>Well it seems like more and more was pulled into the frontend as time passed on, mainly to free up the backend to do the real processing. Like number crunching, data processing and so on. Which makes sense, but now it is weird to say the state of an application should be as close to the user as possible. Also servers are now more than capable of doing both work, and therefore the natural conclusion is that since most of the things were in the frontend anyway now; why not move the whole thing to the frontend?</p>

<p>The thing is that it makes the codebase more difficult to manage, you introduce unwanted complexity let alone making it secure. The whole codebase has so many extra dependencies and vulnerabilities since a lot of Server Side Template Injection (SSTI) plague the landscape now and have been fixed/found already in the old backend languages.</p>

<p>Of course it seems convenient, but let&#39;s take the data models that you want to use. You define them either from the frontend perspective or backend perspective first. In the frontend you might only want first name and an e-mail address, for example. Then in the backend you want to have maybe something more than just first name and an email address. How about a password to login? Or a session id, or maybe just an id in general for the databases so you can store multiple combinations of people? Then you have to transform schemas or build them differently. So you get views or something equivalent and suddenly everything is back to this again.</p>

<p>I would steer away from having the whole application in one language with one library/framework since nothing so far checks all the boxes and covers all the bases.</p>

<p><a href="https://stealthycoder.writeas.com/tag:100DaysToOffload" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">100DaysToOffload</span></a> <a href="https://stealthycoder.writeas.com/tag:frontend" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">frontend</span></a> <a href="https://stealthycoder.writeas.com/tag:JavaScript" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">JavaScript</span></a> <a href="https://stealthycoder.writeas.com/tag:devlife" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">devlife</span></a></p>
]]></content:encoded>
      <guid>https://stealthycoder.writeas.com/frontend-dev-is-best-dev</guid>
      <pubDate>Sun, 08 Jan 2023 23:48:30 +0000</pubDate>
    </item>
  </channel>
</rss>