<?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: Sugriv Lodhi</title>
    <description>The latest articles on DEV Community by Sugriv Lodhi (@sugrivlodhi).</description>
    <link>https://dev.to/sugrivlodhi</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%2F875556%2Fde7e3ea4-113a-4631-ac8d-77e76b544900.jpg</url>
      <title>DEV Community: Sugriv Lodhi</title>
      <link>https://dev.to/sugrivlodhi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sugrivlodhi"/>
    <language>en</language>
    <item>
      <title>A real-life scenario where call, apply, and bind use</title>
      <dc:creator>Sugriv Lodhi</dc:creator>
      <pubDate>Mon, 02 Sep 2024 18:28:03 +0000</pubDate>
      <link>https://dev.to/sugrivlodhi/a-real-life-scenario-where-call-apply-and-bind-use-4525</link>
      <guid>https://dev.to/sugrivlodhi/a-real-life-scenario-where-call-apply-and-bind-use-4525</guid>
      <description>&lt;p&gt;A real-life scenario where call, apply, and bind are useful is when you have objects representing different users, and you want to reuse a method across these objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario: Managing User Permissions&lt;/strong&gt;&lt;br&gt;
   Imagine you have a system where different users have different &lt;br&gt;
   permissions, and you want to check if a user has a certain &lt;br&gt;
   permission.&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%2Frlkaa7pke3ophkq95gb7.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%2Frlkaa7pke3ophkq95gb7.png" alt=" " width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function to Check Permissions&lt;/strong&gt;&lt;br&gt;
You have a function that checks if a user has a certain permission:&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%2Fwfjxjmtz2s8wtmvx93sm.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%2Fwfjxjmtz2s8wtmvx93sm.png" alt=" " width="752" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using call to Check Permissions Immediately&lt;/strong&gt;&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%2F8ltdrqtx82hvtnovdbo6.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%2F8ltdrqtx82hvtnovdbo6.png" alt=" " width="737" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using apply to Check Permissions with Arguments as Array&lt;/strong&gt;&lt;br&gt;
If you had multiple permissions to check at once, you could use apply (though it's less common for single arguments):&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%2F5ky1l688acoiqwz1mbsi.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%2F5ky1l688acoiqwz1mbsi.png" alt=" " width="764" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using bind to Create a New Function for a Specific User&lt;/strong&gt;&lt;br&gt;
You might want to create a specific function for each user to check their permissions without having to pass the user object each time:&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%2Fckpyuh8f1zl6wokc71wb.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%2Fckpyuh8f1zl6wokc71wb.png" alt=" " width="716" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
 *&lt;em&gt;call, apply, and bind *&lt;/em&gt; are powerful methods in JavaScript that allow you to control the context (this value) in which a function is executed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;call: Immediately invokes a function with a specified this value and individual arguments, making it useful for applying methods across different objects in a straightforward manner.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;apply: Similar to call, but accepts arguments as an array, making it more suitable when dealing with variable-length arguments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;bind: Creates a new function with a fixed this value, allowing you to easily reuse functions with a specific context in various parts of your application.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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