<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Santosh Bharti</title>
    <description>The latest articles on DEV Community by Santosh Bharti (@santosh_bharti_97c86158f2).</description>
    <link>https://dev.to/santosh_bharti_97c86158f2</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2957827%2Fff68e10a-6f25-4aae-88d1-2411d0fdbd8f.png</url>
      <title>DEV Community: Santosh Bharti</title>
      <link>https://dev.to/santosh_bharti_97c86158f2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/santosh_bharti_97c86158f2"/>
    <language>en</language>
    <item>
      <title>Polyfill for call in JavaScript</title>
      <dc:creator>Santosh Bharti</dc:creator>
      <pubDate>Thu, 20 Mar 2025 12:37:57 +0000</pubDate>
      <link>https://dev.to/santosh_bharti_97c86158f2/polyfill-for-call-in-javascript-2n5g</link>
      <guid>https://dev.to/santosh_bharti_97c86158f2/polyfill-for-call-in-javascript-2n5g</guid>
      <description>&lt;h2&gt;
  
  
  What is Function.prototype.call?
&lt;/h2&gt;

&lt;p&gt;The call method allows you to invoke a function with a specified &lt;strong&gt;this&lt;/strong&gt; context and pass arguments individually. Here’s a quick example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctegqsslt9kwfecwdodf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctegqsslt9kwfecwdodf.png" alt="Image description" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How we can Implement custom call
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvzcr67li76efibtg6ojc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvzcr67li76efibtg6ojc.png" alt="Image description" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Default Context:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;context = context || globalThis;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If context is &lt;strong&gt;null&lt;/strong&gt; or &lt;strong&gt;undefined&lt;/strong&gt;, it defaults to &lt;strong&gt;globalThis&lt;/strong&gt;. This ensures the function is called in the global context when no context is provided.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating a Unique Key:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;const uniqueKey = Symbol("fn");&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;Symbol&lt;/strong&gt; is used to create a unique property key to avoid overwriting existing properties on the context object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Assigning the Function to the Context:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;context[uniqueKey] = this;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function (this refers to the function being called) is temporarily added as a property of the context object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Calling the Function:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;const result = context[uniqueKey](...args);&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function is called with the provided arguments using the spread syntax.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cleaning Up:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;delete context[uniqueKey];&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The temporary property is removed from the context object to prevent side effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Returning the Result:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;return result;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The result of the function call is returned.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understanding Custom Array.prototype.reduce and Its Implementation</title>
      <dc:creator>Santosh Bharti</dc:creator>
      <pubDate>Wed, 19 Mar 2025 16:35:46 +0000</pubDate>
      <link>https://dev.to/santosh_bharti_97c86158f2/understanding-custom-arrayprototypereduce-and-its-implementation-5dfl</link>
      <guid>https://dev.to/santosh_bharti_97c86158f2/understanding-custom-arrayprototypereduce-and-its-implementation-5dfl</guid>
      <description>&lt;h2&gt;
  
  
  What is Array.prototype.reduce?
&lt;/h2&gt;

&lt;p&gt;The reduce method executes a reducer function (provided by you) on each element of the array, resulting in a single output value. The reducer function takes four arguments:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Accumulator&lt;/strong&gt;: The accumulated value previously returned by the last invocation of the callback, or the initialValue if supplied.&lt;br&gt;
2.&lt;strong&gt;Current Value&lt;/strong&gt;: The current element being processed in the array.&lt;br&gt;
3.&lt;strong&gt;Current Index&lt;/strong&gt;: The index of the current element being processed.&lt;br&gt;
4.&lt;strong&gt;Source Array&lt;/strong&gt;: The array on which reduce was called.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;array.reduce(reducerCallbackFn,initialValue)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets start with basic implementation of reduce
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsqc6bc01en8ethrksduz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsqc6bc01en8ethrksduz.png" alt="Image description" width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Initializing the Accumulator
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;let accumulator=initalValue!==undefined?initalValue:this[0]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The accumulator is the value that will be updated with each iteration of the loop. If an initialValue is provided, it is used as the starting value for the accumulator. otherwise, the first element of the array (&lt;strong&gt;this[0]&lt;/strong&gt;) is used as the initial value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Determining the Start Index
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;let startIndex = initialValue !== undefined ? 0 : 1;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If an initialValue is provided, the loop should start from index &lt;strong&gt;0&lt;/strong&gt; otherwise, it should start from index &lt;strong&gt;1&lt;/strong&gt; because the first element is already used as the initial value for the accumulator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterating Over the Array
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgf2jqqxo5w4bj2d3bb24.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgf2jqqxo5w4bj2d3bb24.png" alt="Image description" width="788" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The loop iterates over the array, starting from the appropriate index. For each element, the callback function is called with the following arguments:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Accumulator&lt;/strong&gt;: The current accumulated value.&lt;br&gt;
2.&lt;strong&gt;Current Value&lt;/strong&gt;: The current element (&lt;strong&gt;this[i]&lt;/strong&gt;).&lt;br&gt;
3.&lt;strong&gt;Current Index&lt;/strong&gt;: The index of the current element (&lt;strong&gt;i&lt;/strong&gt;).&lt;br&gt;
4.&lt;strong&gt;Source Array&lt;/strong&gt;: The array itself (&lt;strong&gt;this&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;The result of the callback function is assigned back to the accumulator.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
