<?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: Victor Ayomipo</title>
    <description>The latest articles on DEV Community by Victor Ayomipo (@vayo).</description>
    <link>https://dev.to/vayo</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%2F1034974%2F74f0bf00-4e8e-4900-bc80-263bb0dc376c.jpeg</url>
      <title>DEV Community: Victor Ayomipo</title>
      <link>https://dev.to/vayo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vayo"/>
    <language>en</language>
    <item>
      <title>How I Built an AI Employee to Run My SaaS Revenue (OpenClaw + Creem)</title>
      <dc:creator>Victor Ayomipo</dc:creator>
      <pubDate>Tue, 31 Mar 2026 22:28:56 +0000</pubDate>
      <link>https://dev.to/vayo/how-i-built-an-ai-employee-to-run-my-saas-revenue-openclaw-creem-5d7d</link>
      <guid>https://dev.to/vayo/how-i-built-an-ai-employee-to-run-my-saas-revenue-openclaw-creem-5d7d</guid>
      <description>&lt;p&gt;I have a confession.&lt;/p&gt;

&lt;p&gt;I have this habit of checking my &lt;a href="https://www.creem.io/" rel="noopener noreferrer"&gt;Creem dashboard&lt;/a&gt; constantly. It gives this opium feeling, seeing people actually paying for what I created. Between builds... I check it. Before bed... I check it.&lt;/p&gt;

&lt;p&gt;So when &lt;a href="https://openclaw.ai/" rel="noopener noreferrer"&gt;OpenClaw&lt;/a&gt; was released, I had an idea. I am someone who uses AI a lot... So why not build one to handle a part of my business?&lt;/p&gt;

&lt;p&gt;So, I built &lt;strong&gt;Alfred&lt;/strong&gt;. An autonomous AI agent powered by OpenClaw that lives in my Discord, watches my Creem store around the clock, and tells me when something needs my attention.&lt;/p&gt;

&lt;p&gt;Division of labor, babyyyy.&lt;/p&gt;

&lt;p&gt;Anyways, let me tell you exactly how I built this AI Operations Manager using OpenClaw and Creem, from A to Z.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/NLBIjCSNi7Y"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  The Architecture: How Alfred’s Brain Works
&lt;/h2&gt;

&lt;p&gt;Apart from managing my saas revenues, there are three main things I set Alfred to do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sends a daily revenue digest&lt;/strong&gt;, i.e., every morning, a clean store health summary by 8 am&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Detects churn&lt;/strong&gt;. He autonomously creates a win-back discount code and drafts a re-engagement email&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Catches failed payments&lt;/strong&gt; and instantly generates a billing portal link and sends it to me to further investigate&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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%2Fr1zi8440ktq0qby7gva2.webp" 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%2Fr1zi8440ktq0qby7gva2.webp" alt=" " width="800" height="681"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the architecture has four main components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Brain (OpenClaw + LLM):&lt;/strong&gt; Powered by OpenRouter (using free advanced models to keep costs at zero, though for heavy tasks, it falls back to Claude or Gemini models).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Tools (Creem Skill):&lt;/strong&gt; A ClawHub-published skill that gives Alfred access to the official Creem CLI and REST API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Nervous System (Security Relay):&lt;/strong&gt; An Express server that securely validates &lt;code&gt;creem-signature&lt;/code&gt; HMACs, deduplicates webhook retries, and taps Alfred on the shoulder when events happen.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Heartbeat (State Tracker):&lt;/strong&gt; A background script to detect anomalies and send daily revenue digests.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: Install OpenClaw
&lt;/h2&gt;

&lt;p&gt;OpenClaw is made to be installed locally on your machine. You don't need a massive server; for now, your laptop works perfectly.&lt;/p&gt;

&lt;p&gt;If you don't have it yet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://openclaw.ai/install.sh | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need Node 22+. Once it runs, you'll hit an onboarding wizard.&lt;/p&gt;

&lt;p&gt;Here's what I picked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Setup mode&lt;/strong&gt;: QuickStart&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Model provider&lt;/strong&gt;: OpenRouter (free-tier models... I'll explain why in a bit)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Channel&lt;/strong&gt;: Discord&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Discord, you'll need to create a bot via the &lt;em&gt;Discord Developer Portal,&lt;/em&gt; name it, and copy the bot token into the setup. The &lt;a href="https://docs.openclaw.ai/channels/discord" rel="noopener noreferrer"&gt;OpenClaw Discord docs&lt;/a&gt; have a clean walkthrough; follow that, it takes maybe 5 minutes.&lt;/p&gt;

&lt;p&gt;Once complete:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openclaw gateway status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the gateway listening on port &lt;code&gt;18789&lt;/code&gt;. That's the heartbeat alive.&lt;/p&gt;

&lt;p&gt;Now, regarding my model of choice, I initially tried Gemini. Got rate-limited in three prompts. I tried Claude. Expensive. Eventually landed on OpenRouter with free high-agentic models. Although for high-level tasks, Claude or GPT is recommended.&lt;/p&gt;

&lt;p&gt;Also, one warning nobody told me is how OpenClaw sends massive context to the LLM on every prompt. We're talking 100k–200k tokens per message because it includes conversation history, skill files, memory, tool schemas... everything. Keep that in mind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Clone the Repo &amp;amp; Install
&lt;/h2&gt;

&lt;p&gt;Now, an agent is just a blank slate until you give it an identity.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To follow along much easier, here is the &lt;a href="https://github.com/vayospot/creem-agent" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt; that contains all you need to have your own Alfred.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/vayospot/creem-agent.git
&lt;span class="nb"&gt;cd &lt;/span&gt;creem-agent
npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you can also install the skill directly from ClawHub:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openclaw skills &lt;span class="nb"&gt;install &lt;/span&gt;creem-agent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More on that later. For now, clone it so you have everything locally.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Copy Files to OpenClaw
&lt;/h2&gt;

&lt;p&gt;OpenClaw usually loads your agent's personality and config from &lt;code&gt;~/.openclaw/workspace/&lt;/code&gt;. The repo contains a &lt;code&gt;template/&lt;/code&gt; folder with everything you need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Copy the Creem skill&lt;/span&gt;
&lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; skills/creem-agent ~/.openclaw/workspace/skills/

&lt;span class="c"&gt;# Copy personality &amp;amp; config templates&lt;/span&gt;
&lt;span class="nb"&gt;cp &lt;/span&gt;template/&lt;span class="k"&gt;*&lt;/span&gt; ~/.openclaw/workspace/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in your OpenClaw workspace, you should have these files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;SOUL.md&lt;/code&gt; — Alfred's personality and how he thinks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;IDENTITY.md&lt;/code&gt; — Who Alfred is (name, role, vibe)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;USER.md&lt;/code&gt; — Who &lt;em&gt;you&lt;/em&gt; are and what you need from him&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;HEARTBEAT.md&lt;/code&gt; — His recurring checklist&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;openclaw.json&lt;/code&gt; — Gateway config (heartbeat interval, Discord, skill loading). This should be in the root folder i.e &lt;code&gt;~/.openclaw/&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the part I love most about OpenClaw. Your agent's entire being is just plain Markdown files.&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%2F4f1prn3n6kva7gnfqfk9.webp" 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%2F4f1prn3n6kva7gnfqfk9.webp" alt="OpenClaw Workspace Directory" width="800" height="543"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Meet Alfred's Brain Files
&lt;/h2&gt;

&lt;p&gt;Here's a quick breakdown of what each file does; the full contents are already in each file as templates, ready to use.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;IDENTITY.md&lt;/code&gt; is Alfred's persona (Who he is):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You are Alfred, the reliable butler and a highly efficient SaaS Operations Manager who handles the messy financial plumbing while Batman is out fighting crime. Highly competent,fiercely loyal.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm not joking. That's literally it. And honestly? It works. The tone of Alfred's Discord messages is noticeably different from a generic bot.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SOUL.md&lt;/code&gt; is Alfred's character and principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Protect MRR. Retention is the primary goal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the Creem API is unreachable, log it, and queue a retry in 5 minutes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lead with data. BLUF (Bottom Line Up Front). No walls of text.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;USER.md&lt;/code&gt; is its onboarding doc, i.e., what Alfred should know about you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Role: SaaS Founder &amp;amp; Developer
Keep all alerts highly actionable.
When drafting customer emails, make them warm, personal, and casual.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The more you fill this out, the more useful Alfred becomes. Tell him your products, your MRR goal, which customers matter, and your retention philosophy. He uses all of it.&lt;/p&gt;

&lt;p&gt;And the more you and Alfred communicate, the more it self-improves and grows autonomously.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Teaching Alfred About Creem
&lt;/h2&gt;

&lt;p&gt;To make Alfred truly advanced and useful, he needs to be able to talk to Creem.&lt;/p&gt;

&lt;p&gt;Here's where I made my first big mistake... and the pivot that actually made everything work.&lt;/p&gt;

&lt;p&gt;Initially, I over-engineered this. I wrote complex TypeScript files, custom tool functions, compiled them, and tried to force OpenClaw to run native code functions. It worked... barely, and just kept breaking.&lt;/p&gt;

&lt;p&gt;The realization was that OpenClaw's skill system is &lt;strong&gt;primarily prompt-based&lt;/strong&gt;. Skills are defined by a &lt;code&gt;SKILL.md&lt;/code&gt; file. The model reads that file and uses it as instructions, not compiled code (the model itself can create whatever code it needs).&lt;/p&gt;

&lt;p&gt;So I deleted all the complex TypeScript and replaced it with one clean &lt;code&gt;SKILL.md&lt;/code&gt; that tells Alfred exactly what to do with Creem.&lt;/p&gt;

&lt;p&gt;The key sections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;STRICT RULES:
&lt;span class="p"&gt;
-&lt;/span&gt; AUTHENTICATION: Before running any creem command, ALWAYS run:creem login --api-key $CREEM_API_KEY
&lt;span class="p"&gt;-&lt;/span&gt; Use ONLY the creem CLI via Exec tool for Creem actions
&lt;span class="p"&gt;-&lt;/span&gt; For failed payment (past_due): run creem customers billing &lt;span class="nt"&gt;&amp;lt;customerId&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; For churn (canceled): create a winback discount and draft a win-back email
&lt;span class="p"&gt;-&lt;/span&gt; If the CLI fails: curl https://creem.io/SKILL.md for live reference
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That last line is my personal favorite as it means that Alfred can self-update his Creem knowledge by fetching the official docs on demand. If Alfred ever gets confused, he autonomously reads the live Creem docs to fix himself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: The Heartbeat
&lt;/h2&gt;

&lt;p&gt;I believe that a true AI employee shouldn't have to wait for you to ask questions. It should proactively monitor the business.&lt;/p&gt;

&lt;p&gt;OpenClaw has a built-in heartbeat system whereby every 30 minutes, it wakes Alfred up and runs through &lt;code&gt;HEARTBEAT.md&lt;/code&gt;. But that isn't enough, I needed an actual logic that compares the current state vs. the last state, detects changes, and flags alerts.&lt;/p&gt;

&lt;p&gt;So to keep things efficient, I wrote a tiny, self-contained Python script (&lt;code&gt;skills/creem-agent/scripts/heartbeat.py&lt;/code&gt;) that Alfred runs when his heartbeat triggers.&lt;/p&gt;

&lt;p&gt;The script:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Loads the previous state from &lt;code&gt;~/.creem/heartbeat-state.json&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Runs &lt;code&gt;creem transactions list --limit 50 --json&lt;/code&gt;, &lt;code&gt;creem subscriptions list --json&lt;/code&gt;, &lt;code&gt;creem customers list --json&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compares against the saved state. New transactions? Status changes? New customers?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If nothing changed, it prints &lt;code&gt;HEARTBEAT_OK&lt;/code&gt; (which tells Alfred to stay silent). If there is a change, it outputs a JSON alert&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Saves the new state for the next cycle&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lastCheckAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-03-28T08:00:00Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lastTransactionId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"txn_xxx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"transactionCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;47&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"subscriptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"active"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"trialing"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"past_due"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"knownSubscriptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"sub_xxx"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"active"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;openclaw.json&lt;/code&gt;, the heartbeat is configured:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"heartbeat"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"every"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"30m"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lightContext"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;lightContext: true&lt;/code&gt; is important as it tells OpenClaw to use a lean context for heartbeats (saves tokens on the routine checks where nothing happened).&lt;/p&gt;

&lt;p&gt;To test it manually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 ~/.openclaw/workspace/skills/creem-agent/scripts/heartbeat.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First run: creates the state file. Normal runs with no changes: outputs &lt;code&gt;HEARTBEAT_OK&lt;/code&gt;. Alfred stays silent. Which is exactly what we want... no noise.&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%2Fsxbvghn0mm3n7epym3xe.webp" 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%2Fsxbvghn0mm3n7epym3xe.webp" alt="Terminal Output of HEARTBEAT_OK response" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: The Nervous System (Webhooks &amp;amp; Security)
&lt;/h2&gt;

&lt;p&gt;Heartbeats catch changes between cycles, but when a payment fails, I need to know &lt;em&gt;now&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Creem sends and signs every webhook with an HMAC-SHA256 signature. OpenClaw can't verify that natively. So we need a &lt;strong&gt;Security Relay&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The relay server (&lt;code&gt;relay/server.ts&lt;/code&gt;) sits between Creem and OpenClaw. It does three critical things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HMAC-SHA256 Signature Verification:&lt;/strong&gt; It verifies the &lt;code&gt;creem-signature&lt;/code&gt; header. If it's not genuinely from Creem, it drops the request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event Deduplication:&lt;/strong&gt; Webhooks retry. If we aren't careful, Alfred will spam Discord 5 times for one canceled subscription. I added an in-memory Map that stores &lt;code&gt;event.id&lt;/code&gt;. If we've seen it, we skip it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Routing to Discord:&lt;/strong&gt; It packages the event into a prompt and forwards it to OpenClaw's internal &lt;code&gt;/hooks/agent&lt;/code&gt; endpoint.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Creem → ngrok URL → Relay Server → verify signature → OpenClaw → Discord
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I faced an issue that cost me 3 hours. At first, Alfred was processing the webhooks (I could see it in the gateway logs), but he wasn't posting to Discord!&lt;/p&gt;

&lt;p&gt;The fix is that the webhook payload to OpenClaw must include the specific channel ID&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp&lt;/span&gt; .env.example .env
&lt;span class="c"&gt;# Fill in: CREEM_API_KEY, CREEM_WEBHOOK_SECRET, DISCORD_BOT_TOKEN, DISCORD_CHANNEL_ID&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start the relay:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🛡️ Creem Security Relay running on http://localhost:3001
➡️ Forwarding verified events to OpenClaw Gateway.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For local testing, expose it with ngrok:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ngrok http 3001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the &lt;code&gt;https://&lt;/code&gt; URL. Go to Creem Dashboard → Developers → Webhooks. Create a webhook, give it a name, paste the ngrok URL + &lt;code&gt;/webhook/creem&lt;/code&gt;, select ALL events and save.&lt;/p&gt;

&lt;p&gt;Then grab the webhook secret from that same page and add it to the &lt;code&gt;CREEM_WEBHOOK_SECRET&lt;/code&gt; in your &lt;code&gt;.env&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Testing Everything
&lt;/h2&gt;

&lt;p&gt;Now, this is the interesting part. Let's test our AI employee.&lt;/p&gt;

&lt;p&gt;Restart OpenClaw so it picks up all the new files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openclaw gateway restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Workflow 1: Natural Language Queries
&lt;/h3&gt;

&lt;p&gt;In your Discord channel, test Alfred:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; &lt;em&gt;"Yo &lt;a class="mentioned-user" href="https://dev.to/alfred"&gt;@alfred&lt;/a&gt;, what's my current MRR and active subscribers?"'&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Alfred:&lt;/strong&gt; &lt;em&gt;"Your MRR is $0.00 with 0 active subscribers. You only have one-time transactions, no recurring subscriptions set up."&lt;/em&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%2F6zerkffi01quytalqscv.webp" 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%2F6zerkffi01quytalqscv.webp" alt="Alfred response on Discord" width="800" height="672"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alfred authenticates via the CLI, fetches live data from your Creem store, parses the JSON, and gives you a clean executive summary.&lt;/p&gt;

&lt;p&gt;Test the heartbeat:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Alfred run heartbeat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First run creates the state file. Subsequent runs either stay silent (&lt;code&gt;HEARTBEAT_OK&lt;/code&gt;) or alert you about changes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Workflow 2: Failed Payment Alerts (Dunning)
&lt;/h4&gt;

&lt;p&gt;I went into the Creem dashboard and manually triggered a &lt;code&gt;past_due&lt;/code&gt; test event. Creem fired the webhook. The Relay verified it. Alfred woke up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Alfred:&lt;/strong&gt; &lt;em&gt;"&lt;/em&gt;&lt;strong&gt;&lt;em&gt;Failed Payment Detected:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;The subscription for&lt;/em&gt; &lt;code&gt;john@example.com&lt;/code&gt; &lt;em&gt;just went past due. I have generated a secure billing portal link for them to update their card:&lt;/em&gt; &lt;code&gt;https://creem.io/portal/cust_123&lt;/code&gt;&lt;em&gt;. Shall I draft an email for you to send?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;He didn't just notify me. He handed me a potential solution to fix the problem.&lt;/p&gt;

&lt;h4&gt;
  
  
  Workflow 3: Churn Detection &amp;amp; Auto-Winback
&lt;/h4&gt;

&lt;p&gt;For easy testing, I simulated a &lt;code&gt;subscription.canceled&lt;/code&gt; event which can be ran locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run simulate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This fires a fake &lt;code&gt;subscription.canceled&lt;/code&gt; webhook at the relay with a valid HMAC signature.&lt;/p&gt;

&lt;p&gt;Alfred gets this and instinctively remembers a rule I sey that &lt;em&gt;"When dealing with Churn, generate a % discount code."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;He responded on discord that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;💔 Churn Detected — Pro Plan
Customer: churning.customer@example.com
MRR lost: -$29/mo

Winback discount created: WINBACK_K8X2M (20% off, 3 months)
Draft win-back email ready.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F2whtswaslz0qas3w4yth.webp" 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%2F2whtswaslz0qas3w4yth.webp" alt="Alfred response with churn alert + discount code on Discord" width="800" height="730"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;A few honest takeaways from building this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenClaw skills are mostly prompt-based, not code-based.&lt;/strong&gt; Save yourself that time and read the docs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The token cost is real.&lt;/strong&gt; OpenClaw consumes tokens like crazy. Save the heavy models for actual work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Agents aren't difficult to build.&lt;/strong&gt; Alfred isn't magic. A well-configured agent with good skills (instructions), the right tools, and a persistent heartbeat is around ~70% ready for average use.&lt;/p&gt;

&lt;p&gt;Anyways, that's the whole deal.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/vayospot/creem-agent" rel="noopener noreferrer"&gt;github.com/vayospot/creem-agent&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://clawhub.ai/vayospot/creem-agent" rel="noopener noreferrer"&gt;&lt;strong&gt;ClawHub&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; &lt;code&gt;openclaw skills install creem-agent&lt;/code&gt;&lt;/p&gt;

</description>
      <category>creem</category>
      <category>openclaw</category>
      <category>automation</category>
      <category>saas</category>
    </item>
    <item>
      <title>I Monetized My VS Code Extension: Building a Payment Workflow Agent with Creem CLI</title>
      <dc:creator>Victor Ayomipo</dc:creator>
      <pubDate>Sat, 28 Mar 2026 18:39:11 +0000</pubDate>
      <link>https://dev.to/vayo/i-monetized-my-vs-code-extension-building-an-autonomous-payment-agent-with-creem-cli-38c5</link>
      <guid>https://dev.to/vayo/i-monetized-my-vs-code-extension-building-an-autonomous-payment-agent-with-creem-cli-38c5</guid>
      <description>&lt;p&gt;Integrating payments into an app usually means dealing with context switching. You write some code for the payment system. You open the browser. Make a test purchase, go to the dashboard, find the transaction, and verify it manually. Then you go back to the editor. Then something else breaks. Back to the browser. Rinse and repeat.&lt;/p&gt;

&lt;p&gt;It's exhausting.&lt;/p&gt;

&lt;p&gt;So what if you never had to leave the terminal? What if you could ask your AI assistant in plain English, &lt;em&gt;"Did that payment go through?"&lt;/em&gt; and it would just... check?&lt;/p&gt;

&lt;p&gt;That's what I built. And this is how I did it.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/HXKUS-81se0"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What We're Building
&lt;/h2&gt;

&lt;p&gt;I have a VS Code extension called &lt;a href="https://marketplace.visualstudio.com/items?itemName=vayospot.snippetfuse" rel="noopener noreferrer"&gt;&lt;strong&gt;SnippetFuse&lt;/strong&gt;&lt;/a&gt;, a smart AI context packaging tool that helps developers structure their code and context before sending it to AI assistants like Claude or ChatGPT. It's free, open source, and has active users.&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%2Fsg9f79r86rad6u0bda28.webp" 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%2Fsg9f79r86rad6u0bda28.webp" alt="VS Code Marketplace page for SnippetFuse" width="800" height="539"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this guide, I'm adding a &lt;strong&gt;Pro tier&lt;/strong&gt; to SnippetFuse, gating two features (PDF Export and Smart Suggestions) behind a one-time license key, powered by &lt;a href="https://www.creem.io/" rel="noopener noreferrer"&gt;Creem (Merchant of Record)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The main tools for this monetization demonstration would be the &lt;a href="https://docs.creem.io/ai/for-humans/cli" rel="noopener noreferrer"&gt;Creem CLI&lt;/a&gt; and Cline AI Agent, which are all free for what we want to do.&lt;/p&gt;

&lt;p&gt;The whole thing is live on a &lt;code&gt;feat/snippetfuse-pro&lt;/code&gt; branch on GitHub. You can check it out to follow along easily. Let's begin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Let the AI Set Up the CLI For You
&lt;/h2&gt;

&lt;p&gt;First thing I did was open Cline and paste this single prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Read https://creem.io/SKILL.md and set up the Creem CLI for me
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thankfully, Creem built their entire documentation to be AI-readable, giving any AI assistant everything it needs to understand Creem, the CLI, and how to work with both.&lt;/p&gt;

&lt;p&gt;So Cline fetched the SKILL.md file, understood the setup steps, and asked me some questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Do you already have a Creem account?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do you prefer Homebrew or npm?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;I like to use Cline Plan mode first as it saves me a lot of headache down the line.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I answered both, switched to Act Mode, and watched Cline install the CLI, run &lt;code&gt;creem --version&lt;/code&gt; to verify (successful), and then check the authentication with &lt;code&gt;creem whoami&lt;/code&gt; which returned &lt;code&gt;⚠ Not logged in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is always recommended to &lt;em&gt;never share your API key with any service, tool, or agent.&lt;/em&gt; So I had to step in to handle the login myself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;creem login
&lt;span class="c"&gt;# Enter your API key from the Creem dashboard&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I pasted my test API key (&lt;code&gt;creem_test_6xI1...&lt;/code&gt;), Creem validated it, and now, the CLI is authenticated and ready.&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%2Fyltjq5zacn12atp176bx.webp" 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%2Fyltjq5zacn12atp176bx.webp" alt="Terminal showing Creem Authenticated Info " width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Create the Product
&lt;/h2&gt;

&lt;p&gt;To monetize SnippetFuse, I needed to sell a License Key.&lt;/p&gt;

&lt;p&gt;To do this, I initially asked Cline to create a product with the license key feature using the Creem CLI, but I discovered that you can't enable add-ons like "License Keys" via the Creem CLI. Those specific features require the dashboard.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;However, for subscription-type products, those can be created entirely using the Creem CLI.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, I quickly accessed the Creem dashboard, created the "SnippetFuse Pro" product, set the pricing at $15, billing type one-time with the License Key feature enabled, and got the payment link.&lt;/p&gt;

&lt;p&gt;Now, I asked Cline to use the Creem CLI to verify if a "SnippetFuse Pro" product exists. It ran the command &lt;code&gt;creem products list --json&lt;/code&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%2Fqq46y72f42okuzu2upjg.webp" 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%2Fqq46y72f42okuzu2upjg.webp" alt="Terminal showing the SnippetFuse Pro product JSON" width="800" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And, Cline got back to me: &lt;em&gt;"SnippetFuse Pro, $15.00 USD, one-time purchase, status: active."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Just like that, I got everything I need to know about my product (price, billing type, product URL, product ID) without touching the browser.&lt;/p&gt;

&lt;p&gt;So let's take it a step further, I asked Cline: &lt;em&gt;"Create a checkout URL for the SnippetFuse Pro product."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And it ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;creem checkouts create &lt;span class="nt"&gt;--product&lt;/span&gt; prod_1ZRv7tBMqhXAMrfMXBUAgK &lt;span class="nt"&gt;--json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CLI returned a live checkout session object with a &lt;code&gt;checkoutUrl&lt;/code&gt; which can be forwarded to a user, and when opened, directs to the SnippetFuse Pro checkout page.&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%2Ff0gcykxx7u8dhi0wikr7.webp" 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%2Ff0gcykxx7u8dhi0wikr7.webp" alt="Terminal showing Checkout Session JSON with checkoutUrl" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should know that the CLI generates &lt;strong&gt;checkout sessions, which are single-use&lt;/strong&gt; (not a reusable payment link). Once a payment is made, that session URL is permanently marked. So generally, for a permanent "Buy" button in your UI, you can either use server-side helpers for creating checkout sessions or use the product's payment link from the dashboard's share button.&lt;/p&gt;

&lt;p&gt;But in this case, if I needed to test again, I'd just tell Cline to &lt;em&gt;"Generate a fresh checkout session."&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Build a Creem MCP Server
&lt;/h2&gt;

&lt;p&gt;I know even the name looks complicated and perhaps... overwhelming. Trust me, it's not.&lt;/p&gt;

&lt;p&gt;MCP Server means &lt;strong&gt;Model Context Protocol Server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A simple way to think of it is that &lt;strong&gt;an MCP server is a menu of tools that you give your AI assistant.&lt;/strong&gt; Each tool is a thing it can call, like a function. Once Cline has access to an MCP server, it can invoke those tools, chain them together autonomously, in context, without you having to copy-paste commands.&lt;/p&gt;

&lt;p&gt;Basically, we're gonna be using a custom MCP Server to plug the Creem CLI into Cline's brain.&lt;/p&gt;

&lt;p&gt;Let's build one.&lt;/p&gt;

&lt;p&gt;I gave Cline the official Cline MCP development docs, the Creem SKILL.md, and the full Creem API documentation, then told it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Build a MCP server that wraps the Creem CLI as callable tools.
Create it in a folder called `creem-mcp` for this project.

The server should expose the necessary creem CLI tools for :
- products
- customers
- subscriptions
- checkouts
- transactions
- configurations
- whoami / switch_mode
- and other relevant ones
---
Follow how it is done in the official cline MCP docs provided.
[paste cline mcp docs here]
---
Each tool executes the CLI command, parses the JSON output, and returns it.
Include error handling — if CLI returns an error, return it as a tool error.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cline built the whole thing: ~18 tools, TypeScript, proper error handling, timeouts, and handles CLI output to JSON. It even generated a README with the exact config to add to Cline's MCP settings.&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%2F830jbpyslmm68gpikt60.webp" 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%2F830jbpyslmm68gpikt60.webp" alt="Creem MCP Server in CLine UI" width="782" height="790"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next was to click the "Configure MCP Servers" in Cline, open the config, and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"creem-mcp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/path/to/snippetfuse/creem-mcp/build/index.js"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"disabled"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So instead of Cline trying to remember terminal commands, it now has 18 dedicated tools like &lt;code&gt;list_transactions&lt;/code&gt; etc., and under the hood, these tools run the Creem CLI (e.g., &lt;code&gt;creem transactions list --json&lt;/code&gt;), parse the output, and return it to the AI.&lt;/p&gt;

&lt;p&gt;Now, let's test it out. I opened a fresh Cline chat and asked: &lt;em&gt;"How many sales did I make today?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And instead of me running any commands, Cline called &lt;code&gt;list_transactions&lt;/code&gt; through the MCP server and responded:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;📊 Today's Sales (March 23, 2026)
You made 4 sales today totaling $63.00 USD!

07:53 AM — bola t (NC) — $15.00 ✅ Paid
07:37 AM — Customer (NG) — $15.00 ✅ Paid
06:48 AM — holly g (NL) — $18.15 ✅ Paid (incl. VAT)
05:49 AM — gd sss (NG) — $15.00 ✅ Paid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fbpxgo7knbiz0f2ostdl1.webp" 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%2Fbpxgo7knbiz0f2ostdl1.webp" alt="MCP response of Summary of Today's Sales" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And just like that, I didn't write a single command. We've set up an AI coding assistant to use the Creem CLI as a native tool. Just natural language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test the Checkout and Verify With the CLI
&lt;/h2&gt;

&lt;p&gt;The license integration in SnippetFuse is already built with an activation modal, VS Code globalState, and calls to the Creem License API. But &lt;em&gt;does it actually work?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's find out.&lt;/p&gt;

&lt;p&gt;I opened the extension in dev mode, clicked Smart Suggestions (a Pro feature), and the Pro modal appeared. Clicked "Get License Key".&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%2Fsgm8edic7nxpwzqu492h.webp" 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%2Fsgm8edic7nxpwzqu492h.webp" alt="SnippetFuse Pro Modal" width="800" height="740"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The checkout page opened in the browser, and I completed a test purchase; it was successful with an order confirmation page.&lt;/p&gt;

&lt;p&gt;Then I proceeded to use the CLI to verify the transaction. I asked Cline, &lt;em&gt;"Check the latest transaction in my Creem store and tell me what you see."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Cline called &lt;code&gt;list_transactions&lt;/code&gt; via the MCP server and reported back:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A successful $15.00 USD one-time payment, status: &lt;strong&gt;paid&lt;/strong&gt;. Customer ID: &lt;code&gt;cust_7cJFr...&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&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%2Fg8gmw2jj23wj558xui7w.gif" 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%2Fg8gmw2jj23wj558xui7w.gif" alt="Transaction Report from the Creem MCP Server" width="720" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which means the CLI has verified that the transaction registered and the payment is confirmed. We built, made a test purchase, verified it with the CLI, and confirmed the purchase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debug the Webhook by Querying Transactions
&lt;/h2&gt;

&lt;p&gt;Unfortunately, I was met with an unexpected bug on my local server.&lt;/p&gt;

&lt;p&gt;SnippetFuse, by nature, has no server as it is a local VSCode extension; however, I wanted to send users a personalized Welcome Email with the license key. And to do this, I need a server with a webhook listener.&lt;/p&gt;

&lt;p&gt;So to test things out, I already built a local Express server (&lt;code&gt;server/index.ts&lt;/code&gt;) which listens for Creem events, verifies the HMAC signature, and processes &lt;code&gt;checkout.completed&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The server goes live by using Ngrok (which exposes a local port by creating a temporary public URL):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;server &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm run dev      &lt;span class="c"&gt;# start webhook server on port 3000&lt;/span&gt;
ngrok http 3000               &lt;span class="c"&gt;# expose it to the internet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ngrok URL goes in the Creem config as a webhook endpoint. HMAC signature verification needs &lt;code&gt;CREEM_WEBHOOK_SECRET&lt;/code&gt;, which is wired into the server.&lt;/p&gt;

&lt;p&gt;Now, remember the test purchase we made earlier, and I mentioned I met a bug; well, the server crashed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✅ Webhook received: checkout.completed
TypeError: Cannot read properties of undefined (reading 'customer')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, I'm thinking, did Creem send bad data? Or is my code broken?&lt;/p&gt;

&lt;p&gt;But since I had the Creem CLI right in the terminal, I just fed the error to Cline and told it: &lt;em&gt;"My webhook server threw an error after I completed a test purchase. Check it out and fix."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And Cline did something smart, it fetched the actual Creem webhook docs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s2"&gt;"https://docs.creem.io/llms-full.txt"&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-A&lt;/span&gt; 50 &lt;span class="s2"&gt;"checkout.completed"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And found that the issue was that my webhook handler was accessing &lt;code&gt;event.data.customer&lt;/code&gt;, but the actual Creem payload structure uses &lt;code&gt;event.object.customer&lt;/code&gt;. My server was accessing the wrong key.&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%2Ffpecy1u9clsj6d2ezk76.webp" 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%2Ffpecy1u9clsj6d2ezk76.webp" alt="Creem Webhook correct payload structure" width="750" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cline fixed it, I restarted the server, made another test purchase, and:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🚀 SnippetFuse webhook server running on port 3000
✅ Webhook received: checkout.completed [Event ID: evt_3mpvXy...]
Customer: fast@example.com
Order ID: ord_5JXnKQ...
🎉 License purchased!
Product: SnippetFuse Pro
License Key: 1HK4A-HHF5F-JVMAL-JQ8OM-4V0I1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that I have the License Key, I pasted it into the activation input in SnippetFuse, clicked Activate, and got a VS Code notification: &lt;em&gt;"SnippetFuse Pro activated successfully!"&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Another classic issue is &lt;em&gt;what if the server had been down during a purchase?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To simulate this, I killed the server with &lt;code&gt;Ctrl+C&lt;/code&gt;, made another test purchase, then asked Cline: &lt;em&gt;"My webhook server was down for a few minutes. What transactions came in that I might have missed?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Using the Creem CLI through the MCP, Cline called &lt;code&gt;list_transactions&lt;/code&gt;, scanned by timestamp, and found the missed transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;New Transaction During Downtime:
Customer: Ragnar Floki (rest@gtub.com) — Norway 🇳🇴
Amount: $15.00 USD
Status: ✅ Paid
Timestamp: March 23, 2026 at 3:21 PM
---
This transaction came through approximately 2 minutes ago and likely missed your webhook since the server was down.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So even if webhook deliveries fail or servers are down, I can still query and audit exactly what happened using the CLI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Manage Your Entire Store From the Terminal
&lt;/h2&gt;

&lt;p&gt;Now that the payment integration is completed and works as expected. I realized that I can talk to the Creem CLI in natural language through Cline (via MCP server).&lt;/p&gt;

&lt;p&gt;Here are some scenarios that I tested.&lt;/p&gt;

&lt;p&gt;I simulated a customer support ticket for a user who emailed saying they've lost access to their license key.&lt;/p&gt;

&lt;p&gt;Instead of opening the dashboard, I asked Cline:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; &lt;em&gt;"Can you look up the customer with email &lt;a href="mailto:jenny.rosen@example.com"&gt;jenny.rosen@example.com&lt;/a&gt; and tell me their purchase history?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cline:&lt;/strong&gt; &lt;em&gt;"Customer found. ID:&lt;/em&gt; &lt;code&gt;cust_1phXGDO...&lt;/code&gt;&lt;em&gt;. They have made 2 successful one-time purchases with a total value of $20.00 USD. No active or past subscriptions were found."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now that I have verified that the user is real, I can just tell my AI Agent to generate a billing portal link for them, and I send the link to them. Ticket Solved.&lt;/p&gt;

&lt;p&gt;Another scenario is the store health check.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; &lt;em&gt;"Give me a summary of the current state of my SnippetFuse Pro store. How many transactions, how many customers do we have, any payment issues, what products are active?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Cline called &lt;code&gt;whoami&lt;/code&gt;, &lt;code&gt;list_products&lt;/code&gt;, &lt;code&gt;list_transactions&lt;/code&gt;, &lt;code&gt;list_customers&lt;/code&gt;, and &lt;code&gt;list_subscriptions&lt;/code&gt;, then synthesized everything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cline:&lt;/strong&gt; &lt;em&gt;"10 transactions. $118.15 total revenue. 9 unique customers across 8 countries. 100% payment success rate. 0 subscriptions (one-time product). ✅ No payment issues."&lt;/em&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%2F2dnev6qlyfdhm08gkei3.webp" 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%2F2dnev6qlyfdhm08gkei3.webp" alt="Creem Store Health Summary via MCP Server" width="800" height="850"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's Cline, an AI agent acting as a store analytics tool, powered entirely by the Creem CLI under the hood.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I think that, even though I could have used the CLI by typing commands, it doesn't beat the convenience of using an AI agent like Cline, Claude, or Cursor, which makes it more efficient and easier to work with.&lt;/p&gt;

&lt;p&gt;I built a paywall, tested checkout, verified transactions, debugged webhook issues, and pulled store analytics... all from within the terminal using Creem CLI and Cline.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can find the full code for this integration, including the custom &lt;code&gt;creem-mcp&lt;/code&gt; server, &lt;code&gt;.clinerules&lt;/code&gt; file, and the SnippetFuse implementation in this &lt;a href="https://github.com/vayospot/snippetfuse/tree/feature/snippetfuse-pro-creem" rel="noopener noreferrer"&gt;GitHub repo (&lt;code&gt;feat/snippetfuse-pro-creem&lt;/code&gt; branch)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check out the official &lt;a href="https://creem.io/SKILL.md" rel="noopener noreferrer"&gt;Creem CLI for Agents Guide&lt;/a&gt; to set this up yourself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you're a VS Code user looking for better AI context management, check out &lt;a href="https://marketplace.visualstudio.com/items?itemName=vayospot.snippetfuse" rel="noopener noreferrer"&gt;SnippetFuse&lt;/a&gt; (it's actually free, don't worry!).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Oh, and you can find me on X (Twitter) —&lt;/em&gt; &lt;a href="https://x.com/vayospot" rel="noopener noreferrer"&gt;&lt;em&gt;@vayospot&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>saas</category>
      <category>creem</category>
      <category>cli</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Stop Paying for Ads: How I Set Up a SaaS Affiliate Program with Creem</title>
      <dc:creator>Victor Ayomipo</dc:creator>
      <pubDate>Fri, 27 Mar 2026 14:30:22 +0000</pubDate>
      <link>https://dev.to/vayo/stop-paying-for-ads-how-i-set-up-a-saas-affiliate-program-with-creem-2ce0</link>
      <guid>https://dev.to/vayo/stop-paying-for-ads-how-i-set-up-a-saas-affiliate-program-with-creem-2ce0</guid>
      <description>&lt;p&gt;The affiliate marketing industry is currently valued at &lt;a href="https://www.hostinger.com/uk/tutorials/affiliate-marketing-statistics#:~:text=The%20global%20affiliate%20marketing%20industry%20is%20valued%20at%20%2418.5%20billion%20and%20is%20projected%20to%20grow%20to%20%2431.7%20billion%20by%202031%20due%20to%20increasing%20adoption%20by%20businesses%20and%20content%20creators." rel="noopener noreferrer"&gt;&lt;strong&gt;$18.5 billion&lt;/strong&gt; in 2026&lt;/a&gt;. Brands report an average return of &lt;strong&gt;$12 for every $1 spent&lt;/strong&gt;. Customers who click through an affiliate partner also repeat purchases at a 21% higher rate. SaaS companies with active affiliate programs see up to a &lt;strong&gt;40% increase in customer retention&lt;/strong&gt;, because affiliate-referred users come in already educated and pre-sold.&lt;/p&gt;

&lt;p&gt;Affiliate marketing isn't a get-rich-quick scheme. It’s a straightforward, performance-based business model where you only pay when you actually make a sale.&lt;/p&gt;

&lt;p&gt;In this guide, I’m going to show you how to set up a complete, automated affiliate program for your SaaS using &lt;strong&gt;Creem’s native Affiliate Hub&lt;/strong&gt; in minutes.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/c1HzHUZXWn0"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;If you prefer watching... I made a full setup walkthrough showing exactly how I set this up for my own SaaS. Otherwise, keep reading, I'll cover everything below&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'll walk you through the setup from both a founder's and an affiliate's perspective and share some hard-learned recruitment strategies.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Creem's Affiliate System Works
&lt;/h2&gt;

&lt;p&gt;Creem is a Merchant of Record (MoR) built specifically for SaaS and digital creators. That means they act as the legal seller of your product, handling stuff like global tax compliance, VAT calculations, subscriptions, and chargebacks, so you don't have to.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.creem.io/features/affiliate-program" rel="noopener noreferrer"&gt;Creem's affiliate platform&lt;/a&gt; has two parts:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;Who uses it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Affiliate Hub&lt;/td&gt;
&lt;td&gt;&lt;code&gt;creem.io/dashboard/affiliate-hub&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;You. The founder&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Affiliate Portal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;affiliates.creem.io&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Your partners. Creators, influencers, users&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Hub is where you create the program, set commissions, invite partners, and track revenue. The Portal is where your affiliates live. They log in, grab their referral link, and track their earnings.&lt;/p&gt;

&lt;p&gt;Creem handles the tracking, attribution, and payouts automatically. You don't calculate anything manually.&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%2Ffxxq3peuaekw21xid262.webp" 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%2Ffxxq3peuaekw21xid262.webp" alt="Overview of Creem Affiliate Hub dashboard" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, let’s get our hands dirty. I’ll be using my own live SaaS, &lt;a href="https://fastary.com/" rel="noopener noreferrer"&gt;Fastary.com&lt;/a&gt;, for this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 1: Set Up Your Store and Product
&lt;/h2&gt;

&lt;p&gt;Before anyone can promote your product, you need something to promote.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enable Live Mode
&lt;/h3&gt;

&lt;p&gt;Head to &lt;a href="https://creem.io/dashboard" rel="noopener noreferrer"&gt;creem.io/dashboard&lt;/a&gt; and create your store. If you already have one, use that instead.&lt;/p&gt;

&lt;p&gt;Here is a critical thing: &lt;strong&gt;Creem's Affiliate Hub is hidden in Test Mode.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're in Test Mode and wondering why you can't find "Affiliate Hub" anywhere in the sidebar, that's why. You need to be in Live Mode to access it. Go to the sidebar and &lt;strong&gt;turn off Test Mode&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%2Fk9zlyufmu0u9rdjkal2h.gif" 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%2Fk9zlyufmu0u9rdjkal2h.gif" alt="Gif of Toggling Test Mode Off to Enable Affiliate Hub in Creem" width="600" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Your Product
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;strong&gt;Commerce &amp;gt; Products&lt;/strong&gt; in the sidebar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;Create Product&lt;/strong&gt; and fill in the necessary details.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once created, click the &lt;strong&gt;Share&lt;/strong&gt; button to copy your payment link (it looks like &lt;code&gt;https://www.creem.io/payment/prod_xxx&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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%2Fnwowrongaq8ir7czp58v.webp" 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%2Fnwowrongaq8ir7czp58v.webp" alt="Creem Products Page showing the 'Share' button and Payment link" width="800" height="835"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, update the pricing buttons on your SaaS website with this link. That's your checkout.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;For deeper integration&lt;/strong&gt; of connecting Creem to your backend, handling webhooks, the whole thing, check out the &lt;a href="https://docs.creem.io/api-reference/introduction" rel="noopener noreferrer"&gt;Creem API docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But for this guide and for most founders starting out, the payment link alone handles everything.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Phase 2: Create Your Affiliate Program
&lt;/h2&gt;

&lt;p&gt;Again, &lt;strong&gt;the Affiliate Hub is currently only visible in Live Mode.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In your sidebar, navigate to &lt;strong&gt;Growth &amp;gt; AffiliateHub&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's how I filled mine out for my SaaS, Fastary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Program Name:&lt;/strong&gt; &lt;em&gt;“Fastary Partner Program”&lt;/em&gt;. I used the word 'Partner' instead of 'Affiliate' as it makes creators feel like a key part of the product, not an afterthought.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Website URL:&lt;/strong&gt; Your landing page. This is where referral links will send people. For me, &lt;code&gt;https://fastary.com/&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; This is your pitch to potential affiliates. I wrote: &lt;em&gt;"Refer Fastary to your friends and followers, and Earn 25% of all payments. There is no limit to how much you can earn."&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;Next.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Program Slug:&lt;/strong&gt; I used &lt;code&gt;partners-program&lt;/code&gt;. I kept this evergreen so it doesn't look expired six months from now. &lt;em&gt;(Note: You can't change this later!)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Commission Rate:&lt;/strong&gt; I went with &lt;strong&gt;25%&lt;/strong&gt;. I chose this because for SaaS, 20–30% recurring is the industry standard. But, Saas differs, so choose whatever suits your brand.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Fpfxqwwi7vk0ppb7u5fwb.webp" 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%2Fpfxqwwi7vk0ppb7u5fwb.webp" alt="The Affiliate Hub program setup form filled out with Fastary details" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Create Program&lt;/strong&gt;. And that's it, your affiliate program is officially live.&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%2Fttathb1f5blgcyvjlnvg.webp" 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%2Fttathb1f5blgcyvjlnvg.webp" alt="Creem's Affiliate Hub dashboard" width="800" height="584"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once affiliates are running, here's what to track:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;What It Actually Tells You&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Unique Leads&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;New visitors arriving via affiliate links&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total Clicks&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;All clicks, including repeat visits from the same person&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Checkouts Created&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;People who started a checkout&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Conversions&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Completed purchases&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Conversion Rate&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;% of leads that actually bought. This is your program's health score&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A healthy conversion rate for SaaS affiliates sits around &lt;strong&gt;2–5%.&lt;/strong&gt; If you're consistently below that, the issue is usually one of two things: the wrong affiliates (mismatched audience) or a landing page that isn't converting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 3: Invite Your First Affiliate
&lt;/h2&gt;

&lt;p&gt;Now, we need someone to actually join... to test the full flow.&lt;/p&gt;

&lt;p&gt;But there's a problem: we're in Live Mode, which means Creem expects real credit cards and real money. But we need to test this flow to make sure our partners actually get credited for sales.&lt;/p&gt;

&lt;p&gt;How do we test a live system without paying with real money?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We create a 100% discount code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This lets us simulate a complete checkout, with real tracking, without charging a single card.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In your Creem dashboard, go to &lt;strong&gt;Commerce &amp;gt; Discounts&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;Create Discount&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Name: "Affiliate Testing Internal" &lt;em&gt;(label it clearly so you don't accidentally share it)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code: You can generate a random one &lt;em&gt;(keep it private for obvious reasons)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Discount Type: Percentage with an amount of 100%&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Products: Your SaaS product&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CRITICAL STEP:&lt;/strong&gt; I limit the number of redemptions to &lt;strong&gt;2&lt;/strong&gt;. This ensures that if the code accidentally leaks, nobody can bankrupt the business.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Click &lt;strong&gt;Create Discount&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%2Fdeevdla11qgfflojablo.webp" 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%2Fdeevdla11qgfflojablo.webp" alt="Creem Discount Screen" width="800" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's invite ourselves to the program.&lt;/p&gt;

&lt;p&gt;In the Affiliate Hub, click &lt;strong&gt;"+ Invite Partner".&lt;/strong&gt; Enter an email &lt;em&gt;(don't use your main Creem email)&lt;/em&gt; and a name, then hit &lt;strong&gt;Send Invite.&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%2Fjytphvqx7oak4us7a8s6.webp" 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%2Fjytphvqx7oak4us7a8s6.webp" alt="Creem's Invite Partner modal in the Founder dashboard" width="800" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creem automatically sends a branded invitation email on our behalf. The affiliate receives an email with the subject: &lt;em&gt;"You're invited to join [Your Program Name] on Creem."&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 4: The Affiliate's Experience
&lt;/h2&gt;

&lt;p&gt;Let's switch perspectives. You are now the affiliate.&lt;/p&gt;

&lt;p&gt;Now, open an &lt;strong&gt;Incognito Window&lt;/strong&gt; in your browser. We want a fresh instance so nothing gets mixed up.&lt;/p&gt;

&lt;p&gt;Check the email inbox we sent an invite to. You’ll see a branded email from Creem saying: &lt;em&gt;"You're invited to join Fastary Partner Program."&lt;/em&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%2F274fl0oe9yi2vr7quqdy.webp" 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%2F274fl0oe9yi2vr7quqdy.webp" alt="The Creem Invitation Email received in the inbox" width="800" height="686"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Accept Invite&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Creem will redirect you to &lt;a href="https://affiliates.creem.io" rel="noopener noreferrer"&gt;affiliates.creem.io&lt;/a&gt; and ask you to sign in. Once signed in, you'd be prompted to complete your profile (Name, Bio, Website, Social Links).&lt;/p&gt;

&lt;p&gt;And that's it, you land on the &lt;strong&gt;Affiliate Portal&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Affiliate Portal
&lt;/h3&gt;

&lt;p&gt;The Creem Affiliate Portal dashboard shows everything you need to know as an affiliate.&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%2Fow4h4mcgy8u022flxljp.webp" 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%2Fow4h4mcgy8u022flxljp.webp" alt="The Creem Affiliate Portal dashboard" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right at the top is the golden ticket: &lt;strong&gt;The Referral Link.&lt;/strong&gt; It looks like &lt;code&gt;https://creem.io/affiliate?code=AFFYORIOBLDU&lt;/code&gt;. This is what you, as an affiliate, will put in your YouTube descriptions, tweets or email newsletters.&lt;/p&gt;

&lt;p&gt;Below that, you can see other metrics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Total Revenue:&lt;/strong&gt; How much money you've brought in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unique Customers:&lt;/strong&gt; How many people bought through your link&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clicks:&lt;/strong&gt; How many people clicked your link.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conversion Rate:&lt;/strong&gt; The percentage of clicks that turned into buyers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the sidebar...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Balance&lt;/strong&gt; shows your earnings breakdown: Available &lt;em&gt;(ready to withdraw)&lt;/em&gt;, On Hold &lt;em&gt;(pending review period)&lt;/em&gt;, and Pending Payouts &lt;em&gt;(being processed)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To withdraw, you need to complete a quick &lt;strong&gt;KYC verification&lt;/strong&gt; (ID + selfie, typically done in under 24 hours), then set up a payout method (bank transfer or crypto).&lt;/p&gt;

&lt;p&gt;Payouts run according to the founder (merchant) schedule, but typically the 1st and 15th of every month.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 5: How the Tracking Actually Works
&lt;/h2&gt;

&lt;p&gt;Let's see what exactly happens when someone clicks our affiliate link.&lt;/p&gt;

&lt;p&gt;Copy the referral link and open it in a new tab. You'd notice that the link redirects to &lt;code&gt;fastary.com&lt;/code&gt; and the affiliate code disappears from the URL.&lt;/p&gt;

&lt;p&gt;This is intentional. Creem does this so that the link looks "normal" to your audience, as there's no visible tracking code, which builds trust.&lt;/p&gt;

&lt;p&gt;But, under the hood:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The moment the link is clicked, Creem reads the &lt;code&gt;?code=&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creem quietly attaches a &lt;strong&gt;Cookie&lt;/strong&gt; (like a VIP ticket) to the visitor's browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So even if the visitor closes the tab, goes away and comes back a week later, that ticket is still there (for typically 180 days).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When they click "Subscribe" and open the Creem Checkout, Creem sees the ticket, matches it to your referral code, and says: &lt;em&gt;"Yes. 25% of this sale goes to you."&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's prove it and test the checkout&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;On your SaaS site, click your pricing button to open the Creem checkout.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fill in a test email and name (You can leave the credit card blank).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;Add discount code&lt;/strong&gt; and paste the 100% off code we made earlier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The total drops to &lt;strong&gt;$0.00&lt;/strong&gt;. Click &lt;strong&gt;Pay&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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%2Fl0qbcu1iqh8c8g326jhr.gif" 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%2Fl0qbcu1iqh8c8g326jhr.gif" alt="Gif of Applying 100% discount code at Checkout" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Results (Analytics)
&lt;/h3&gt;

&lt;p&gt;Now, let's look at the data&lt;/p&gt;

&lt;p&gt;Go back to the &lt;strong&gt;Affiliate's Dashboard&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Refresh the page. You will immediately see that the &lt;strong&gt;Clicks&lt;/strong&gt; and &lt;strong&gt;Conversion Rate&lt;/strong&gt; metrics have been updated! But the &lt;strong&gt;Total Revenue&lt;/strong&gt; is still $0.00 because 25% of a $0.00 transaction is $0.00. But the spike in the conversion metric proves the tracking works perfectly.&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%2Fsr875tbxkk7r8w8t0key.webp" 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%2Fsr875tbxkk7r8w8t0key.webp" alt="The Affiliate's Portal dashboard showing the new conversion" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, go back to the &lt;strong&gt;Founder's Affiliate Hub.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You'd notice that the dashboard metrics have updated. If you look at the "Affiliate Partners" table. You will see the Clicks and Conversions also updated in real-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%2Fzdbppfrian7pn7izxkhq.webp" 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%2Fzdbppfrian7pn7izxkhq.webp" alt="The Founder's Affiliate Hub dashboard showing the new conversions" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When customers buy, the revenue automatically splits. You, as the founder, don't have to calculate spreadsheets or wire money manually... Creem handles it all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding Your First Affiliates
&lt;/h2&gt;

&lt;p&gt;Now, the hard part. Getting people to actually join and promote.&lt;/p&gt;

&lt;p&gt;The reason several startup affiliate programs fail is that founders treat affiliates like cheap labour. They offer little one-time commission, drop a link, and wonder why nobody is promoting their product.&lt;/p&gt;

&lt;p&gt;You have to hunt for your affiliates; you have to find them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Your best affiliates are people already paying you. Email and pitch to your most active users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't call them Affiliates. Call them Partners. It changes the psychology. It makes them feel part of the team.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A 10,000-subscriber newsletter in your exact niche will convert at 3-5%. A 500k-follower generalist account might convert at 0.3%. Go niche, go deep.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Know your EPC (Earnings Per Click). Partners want to know: "&lt;em&gt;If I send 100 people to your site, how much money will I make?"&lt;/em&gt; Know that number. Lead with it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't just hand them a link. Give them something to share. It could be a Google Drive folder with your logos, banners, images, and high-converting copy. Make it really easy for them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep in touch with them. When they make their first sale, send them a personalised email congratulating them. That tiny dopamine hit will make them want to promote you again tomorrow.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The market is at &lt;strong&gt;$18.5 billion&lt;/strong&gt; for a reason. Affiliate-driven growth compounds; every affiliate you activate is a distribution channel running 24/7.&lt;/p&gt;

&lt;p&gt;Plus, Creem's native affiliate program already removes the technical friction by handling cookie tracking, revenue splits, international tax compliance, and payouts.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Oh, and don't forget to delete that 100% discount code!).&lt;/em&gt;&lt;/p&gt;

</description>
      <category>saas</category>
      <category>marketing</category>
      <category>tutorial</category>
      <category>creem</category>
    </item>
    <item>
      <title>Explaining "this" Keyword In JavaScript Like You're Five</title>
      <dc:creator>Victor Ayomipo</dc:creator>
      <pubDate>Mon, 27 Feb 2023 17:58:11 +0000</pubDate>
      <link>https://dev.to/vayo/explaining-this-keyword-in-javascript-like-youre-five-191</link>
      <guid>https://dev.to/vayo/explaining-this-keyword-in-javascript-like-youre-five-191</guid>
      <description>&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is one of the most difficult JavaScript concepts to understand, no doubt about that. If you understood it on your first exposure to it, you're probably one of the greatest JavaScript prodigies to ever walk this earth. Although, if you can't seem to wrap your head around it, don't worry, I'd make you a prodigy.&lt;/p&gt;

&lt;p&gt;In this article, You are going to see a substantial amount of code snippets and visual illustrations, this is just to give you a better understanding of JavaScript &lt;code&gt;this&lt;/code&gt; keyword. The code snippets are not complex, I promise.&lt;/p&gt;

&lt;h2&gt;
  
  
  You will learn
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What &lt;code&gt;this&lt;/code&gt; means in JavaScript and how it works under the hood&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in the global scope&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in functions and arrow functions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in object methods&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in constructor functions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in classes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in event handlers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Best practices when using &lt;code&gt;this&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Imagine you are at a smartphone store trying to purchase the latest Samsung device. You found the exact Samsung device and went to the checkout line to finalize the purchase. As you approach the cashier, you noticed a long queue of people waiting to make their own purchases.&lt;/p&gt;

&lt;p&gt;After a few minutes, it got to your turn and you show the cashier the Samsung phone you want to purchase, the cashier then begins the checkout process. As the cashier scans the phone and begins to process your payment, you notice that he keeps using the word "this". The cashier writes some stuff on the receipt like "this phone is the latest model" and "this purchase was paid through cash". It made sense to you because he was referring specifically to the phone you were purchasing, not any other Samsung device in the store.&lt;/p&gt;

&lt;p&gt;In JavaScript, &lt;code&gt;this&lt;/code&gt; works in a similar way. When you call a function using an object, &lt;code&gt;this&lt;/code&gt; refers to the object. So, just as the cashier uses "this" to refer to the specific Samsung device you want to buy, &lt;code&gt;this&lt;/code&gt; in your JavaScript code refers to the object that invoked the function.&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%2Fnt99ya5ari47rjyzhyiq.jpg" 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%2Fnt99ya5ari47rjyzhyiq.jpg" alt="Visual illustration of how " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the illustration above, when the Samsung object calls the function, &lt;code&gt;this&lt;/code&gt; in that function references the Samsung object; when the iPhone object calls the function, &lt;code&gt;this&lt;/code&gt; references the iPhone object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; refers to the object that calls the function at the time of the function call. &lt;code&gt;this&lt;/code&gt; keyword always refers to either an object, undefined or null. Nothing else.&lt;/p&gt;

&lt;p&gt;The code snippet below is a quick example of how &lt;code&gt;this&lt;/code&gt; works -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;thisValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$900&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;thisValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thisValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;thisValue&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// Output: newPhone {name: "Samsung S23 Ultra", price: "$900", thisValue: ƒ}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;thisValue&lt;/code&gt; function was added to the &lt;code&gt;newPhone&lt;/code&gt; object as a method (functions in an object). Therefore, when the &lt;code&gt;newPhone&lt;/code&gt; object invokes the &lt;code&gt;thisValue&lt;/code&gt; method, it returns &lt;code&gt;newPhone&lt;/code&gt; since &lt;code&gt;this&lt;/code&gt; refers to the object that calls a function.&lt;/p&gt;

&lt;p&gt;Now, let's imagine you want to access the brand of the &lt;code&gt;newPhone&lt;/code&gt; object, you could create a method that returns the &lt;code&gt;newPhone&lt;/code&gt; brand, like this &lt;code&gt;newPhone.name&lt;/code&gt;(works!) or you could say &lt;code&gt;this.name&lt;/code&gt;(also works!)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$900&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;thisName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`I own the &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// using object name&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// or&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$900&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;thisName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`I own the &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// using this keyword&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, there's a slight problem. Since everything in JavaScript (e.g array, functions, etc.) are all objects under the hood, there are times you'd want an object to be created from another object. For example, an object called &lt;code&gt;oldPhone&lt;/code&gt; can be created from an existing object &lt;code&gt;newPhone&lt;/code&gt;, like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;touchScreen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;thisName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`I own the &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//Object.create() creates an object based on an existing object.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung J5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;thisName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// outputs: ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think &lt;code&gt;oldPhone.thisName()&lt;/code&gt; would output? It outputs: "I own the Samsung S23 Ultra." which shouldn't be because we need the &lt;code&gt;oldPhone&lt;/code&gt; name and not the &lt;code&gt;newPhone&lt;/code&gt; name. Since &lt;code&gt;thisName()&lt;/code&gt; method is already explicitly tied to only return &lt;code&gt;newPhone&lt;/code&gt; name, trying to call the function with another object (in this case &lt;code&gt;oldPhone&lt;/code&gt; object) would still return &lt;code&gt;newPhone.name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To correct this, &lt;code&gt;thisName()&lt;/code&gt; method should reference the name property using &lt;code&gt;this.name&lt;/code&gt; rather than &lt;code&gt;newPhone.name&lt;/code&gt; so that it works correctly with any object that has a name property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;touchScreen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;thisName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`I own the &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// changed to this.name&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung J5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;thisName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// outputs: I own the Samsung J5. Works as expected&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;this.name&lt;/code&gt; instead of &lt;code&gt;newPhone.name&lt;/code&gt; is better because it allows the code to be more flexible and reusable. Just know that when you use &lt;code&gt;this&lt;/code&gt; in an object method, &lt;code&gt;this&lt;/code&gt; would mostly refer to the object that invokes the method (except for arrow functions, more on that later).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Did you know?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The &lt;code&gt;this&lt;/code&gt; keyword is like a hidden parameter that is passed to a function when it is called. You can set the value of &lt;code&gt;this&lt;/code&gt; to a specific object by using the dot notation (.) to prefix the function with the object. This way, the object that you specified will be used as the &lt;code&gt;this&lt;/code&gt; context, for example &lt;code&gt;newPhone.thisValue()&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It's a powerful feature of the language, but it can be tricky to use correctly, so make sure you understand it well.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; In Global Scope
&lt;/h3&gt;

&lt;p&gt;Before we begin, let's have a quick understanding of how &lt;code&gt;this&lt;/code&gt; works in JavaScript strict mode.&lt;/p&gt;

&lt;p&gt;Strict mode is a mode that was introduced to JavaScript for the main purpose of eliminating silent errors and allowing developers to write clean code. You can apply strict mode to your JavaScript by adding &lt;code&gt;'use strict';&lt;/code&gt; at the top level of your JavaScript i.e before you start coding or doing anything else or at the top level of a function block if you prefer not to apply it to your whole JavaScript code.&lt;/p&gt;

&lt;p&gt;Whether in strict mode or not, &lt;code&gt;this&lt;/code&gt; keyword used at the top level of a script will always refer to the global object(or window object).&lt;/p&gt;

&lt;p&gt;Although, when strict mode is used inside a function, and the function is invoked without an object attached to it, &lt;code&gt;this&lt;/code&gt; would return undefined instead of the global object as the normal JavaScript non-strict mode does. This makes it much easier to spot bugs in the long run.&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%2Fhtvc31ye70mcat2ts6bn.jpg" 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%2Fhtvc31ye70mcat2ts6bn.jpg" alt="Visual illustration of what " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's see how &lt;code&gt;this&lt;/code&gt; works in terms of JavaScript scoping. In the global scope, &lt;code&gt;this&lt;/code&gt; refers to the global object. In a browser environment, the global object is the &lt;code&gt;window&lt;/code&gt; object, while in a Node.js environment, it is the global object.&lt;/p&gt;

&lt;p&gt;These options...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;variables and object properties that are given the value &lt;code&gt;this&lt;/code&gt; in the global scope,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;functions that are invoked in the global scope with no object attached to it,&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...they all return the global object when assigned to the &lt;code&gt;this&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foo2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the variable &lt;code&gt;foo&lt;/code&gt; has &lt;code&gt;this&lt;/code&gt; keyword assigned to it and since the variable was created in the global scope, the &lt;code&gt;this&lt;/code&gt; keyword can't refer to any other object so it defaults to the global object.&lt;/p&gt;

&lt;p&gt;The variable &lt;code&gt;foo1&lt;/code&gt; is an object with one of its properties assigned to the &lt;code&gt;this&lt;/code&gt; keyword. It can be a little tricky since &lt;code&gt;this&lt;/code&gt; mostly references objects but you have to know that, it is only functions found in an object that works that way. Object properties (not object methods) that are assigned &lt;code&gt;this&lt;/code&gt; refers to the global object because &lt;code&gt;this&lt;/code&gt; is not inside a method, it's just in the global scope.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;foo2&lt;/code&gt; function acts differently though. A function that returns the value of &lt;code&gt;this&lt;/code&gt; depends on how it's invoked; since the &lt;code&gt;foo2&lt;/code&gt; function was not invoked as a method of any object, the &lt;code&gt;this&lt;/code&gt; keyword in the function defaults to undefined (strict mode) or the global object (non-strict mode).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; In Functions &amp;amp; Arrow Functions
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;this&lt;/code&gt; in a normal function is actually straightforward, the &lt;code&gt;this&lt;/code&gt; keyword in the function would refer to the object that called it or the object it's bound to, a quick example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create a function that returns `this`&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is playing music.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//create objects&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung J5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//copy the function as methods to the object created&lt;/span&gt;
&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playMusic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playMusic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;//Samsung S23 Ultra is playing music.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;//Samsung J5 is playing music.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the function was copied as a method to the two objects, you might have thought "why not invoke it?". Well if the function was invoked before storing it inside the object, it is the returned value of the function that'd be stored in the object, not the function itself. In essence, doing that would just store a string to &lt;code&gt;newPhone.playMusic&lt;/code&gt; that'd output "[object Window] is playing music." because the function was called with no object attached to it so &lt;code&gt;this&lt;/code&gt; would default to the global object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;❕ Useful Tips&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In functions, this refers to the dynamic context in which that particular function is called:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;It can reference the object that invoked it.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;It can be explicitly bound to a different object than the one that created it by using call(), apply(), or bind().&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;If invoked without the function binding to any object implicitly or explicitly, it references the global object.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, going through the steps of creating a function with &lt;code&gt;this&lt;/code&gt; keyword and copying it as a method to different objects can be quite stressful. Instead, you can just create the function as an object method in one object and make other objects call the object method anytime. To achieve this, Javascript has 3 in-built functions that can help with that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;call&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;apply&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;bind&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These three functions can come in handy when you have an object method that uses &lt;code&gt;this&lt;/code&gt; keyword to reference and update stuff around that object, but you also need a similar object method like that for other objects. Therefore, rather than creating original object methods for each object, you can simply borrow the object method you need from the object that already owns it. These methods can surely save you a ton of time and effort when working with complicated objects or extensive codebases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Call
&lt;/h3&gt;

&lt;p&gt;The call function executes a function and explicitly set the &lt;code&gt;this&lt;/code&gt; keyword to reference a desired object. The syntax is &lt;code&gt;call(thisArg, ...arguments)&lt;/code&gt; where "thisArg" is the object you want &lt;code&gt;this&lt;/code&gt; keyword to refer to and "...arguments" are other arguments(separated by a comma) that the object method might need. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//object with the playMusic method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is playing music.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// objects that need the playMusic method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;momPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dadPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung A80&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//using playMusic method for other objects that needs it&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;momPhone&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//Samsung S10 is playing music.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dadPhone&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//Samsung A80 is playing music.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippets above have three objects; two objects &lt;code&gt;momPhone&lt;/code&gt; and &lt;code&gt;dadPhone&lt;/code&gt; do not have a &lt;code&gt;playMusic&lt;/code&gt; method, only one object &lt;code&gt;newPhone&lt;/code&gt; has a &lt;code&gt;playMusic&lt;/code&gt; method. Now, obviously you'd want all phone objects to be able to play music but creating &lt;code&gt;playMusic&lt;/code&gt; method for each object can be tasking when dealing with lots of objects. Instead, we kind of took a copy of the &lt;code&gt;playMusic&lt;/code&gt; method from the object that has it and used it for the two other objects that needed it. The &lt;code&gt;call()&lt;/code&gt; method makes &lt;code&gt;this&lt;/code&gt; keyword inside the &lt;code&gt;playMusic&lt;/code&gt; method to reference the desired object that was passed as an argument to &lt;code&gt;call()&lt;/code&gt;, instead of the original object that created the method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using apply()
&lt;/h3&gt;

&lt;p&gt;The apply function also invokes an object method that uses &lt;code&gt;this&lt;/code&gt; keyword and binds it to another object. Although, arguments to be used by &lt;code&gt;apply()&lt;/code&gt; are passed as a single array unlike &lt;code&gt;call()&lt;/code&gt;. The syntax is &lt;code&gt;apply(thisArg, [...arguments])&lt;/code&gt; where "thisArg" is the object you want &lt;code&gt;this&lt;/code&gt; keyword to refer to and "[...arguments]" are other arguments that are passed in an array form that the object method might need. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//object with the playMusic method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;song&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;artist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is playing "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;song&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" by &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;artist&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// objects that need the playMusic method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung J5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dadPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung A80&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//using playMusic method for other objects that needs it&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Calm down&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rema&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt; &lt;span class="c1"&gt;//Samsung J5 is playing "Calm down" by Rema.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playMusic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dadPhone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Michael Jackson&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt; &lt;span class="c1"&gt;//Samsung A80 is playing "Bad" by Michael Jackson.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;apply()&lt;/code&gt; in the code above, we were able to use &lt;code&gt;this&lt;/code&gt; keyword from the &lt;code&gt;playMusic&lt;/code&gt; method to reference our desired objects rather than the object that owns the &lt;code&gt;playMusic&lt;/code&gt; method. We also passed in an array argument to tweak the result a little bit.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Differences between call() and apply()&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;They are both incredibly similar with just one difference, which is the way arguments are passed to them.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;call() accepts arguments in list-comma separated form -&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;call(thisArg, arg1, arg2, arg3)&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;apply() accepts arguments in a single array-list form -&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;apply(thisArg, [arg1, arg2, arg3])&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Yes, yes, I know what you're thinking. Why would you want to pass an array as an argument when you could just follow the normal way that &lt;code&gt;call()&lt;/code&gt; uses?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Well, there are instances when you'd need to use &lt;code&gt;apply()&lt;/code&gt; instead of &lt;code&gt;call()&lt;/code&gt;. For instance,&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;The arguments to be used are being passed dynamically by a server so you don't know the fixed number of arguments to pass.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;The arguments you want to use have already been stored in an array.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;You might want to use the length of the arguments you passed.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Using &lt;code&gt;apply()&lt;/code&gt; when you come across any of these scenarios would make your JavaScript journey easier.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Use bind()
&lt;/h3&gt;

&lt;p&gt;If you noticed, when we were using the &lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt; functions on the object method, the object method was invoked immediately to reference the desired object as the &lt;code&gt;this&lt;/code&gt; value. However, in some cases, it might be necessary to bind the &lt;code&gt;this&lt;/code&gt; value of an object method to a new object without invoking it immediately. In such situations, the &lt;code&gt;bind()&lt;/code&gt; function can be used.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; function creates a new function whereby the object method that uses the &lt;code&gt;this&lt;/code&gt; keyword would be bound to the desired object passed as an argument. The returned function can then be invoked anytime you wish and &lt;code&gt;this&lt;/code&gt; would always refer to the desired object that was bounded.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; syntax is &lt;code&gt;bind(thisArg, ...arguments)&lt;/code&gt; where the "thisArg" parameter is the desired object that &lt;code&gt;this&lt;/code&gt; should refer to &amp;amp; the "...arguments" parameters are the list of arguments(separated by commas) that the function might need.&lt;/p&gt;

&lt;p&gt;One of the basic uses of bind() is to create a function that will always be called with a specific value of &lt;code&gt;this&lt;/code&gt;, regardless of how it is invoked. &lt;code&gt;this&lt;/code&gt; would never refer to any other object apart from the desired object it was bound to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//object with the accessInternet method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;accessInternet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; can access the internet.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; can't access the internet.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// object that needs the accessInternet method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung J5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//bind accessInternet method to another object using bind().&lt;/span&gt;
&lt;span class="c1"&gt;//and store the newly created function in a variable&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhoneBound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accessInternet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// "oldPhoneBound" function can now be called in any context and at any time,&lt;/span&gt;
&lt;span class="c1"&gt;// its `this` value would always refer to the "oldPhone" object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can think of &lt;code&gt;bind()&lt;/code&gt; working like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhoneBound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accessInternet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript creates a function for you, then uses &lt;code&gt;call()&lt;/code&gt; to tie the object method &lt;code&gt;this&lt;/code&gt; to the desired object. Since codes placed in a function would not run until the function is invoked, &lt;code&gt;bind()&lt;/code&gt; makes use of that phenomenon&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It creates a function,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the new function body, it places a &lt;code&gt;call()&lt;/code&gt; function that ties the object method &lt;code&gt;this&lt;/code&gt; to the desired object and returns it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The new function can then be stored in any variable and called at any particular time.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  When to use bind()
&lt;/h4&gt;

&lt;p&gt;For instance, when using &lt;code&gt;this&lt;/code&gt; in a setTimeout, it sometimes doesn't work as expected because setTimeout invokes the function you provided it automatically after a given time.&lt;/p&gt;

&lt;p&gt;When setTimeout calls an object method that uses &lt;code&gt;this&lt;/code&gt;, the &lt;code&gt;this&lt;/code&gt; keyword would refer to the global object because setTimeout was what called the object method, not the object. Remember, &lt;code&gt;this&lt;/code&gt; in a function is determined by how the function is called. However, to always make sure &lt;code&gt;this&lt;/code&gt; points to the right object, &lt;code&gt;bind()&lt;/code&gt; comes to the rescue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;accessInternet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung J5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;accessInternet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Global object.&lt;/span&gt;
&lt;span class="c1"&gt;// "oldPhone.hasAccess" was called by setTimeout, &lt;/span&gt;
&lt;span class="c1"&gt;// `this` loses its context because...&lt;/span&gt;
&lt;span class="c1"&gt;//  oldPhone didn't call the function, setTimeout did...&lt;/span&gt;
&lt;span class="c1"&gt;// `this` defaults to the global object.&lt;/span&gt;


&lt;span class="c1"&gt;//To fix this&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;accessInternet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung J5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;//bind "this" to the desired object&lt;/span&gt;
&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;accessInternet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//oldPhone Object&lt;/span&gt;

&lt;span class="c1"&gt;// If call() had been used, the function would have been invoked before you even get a chance to use setTimeout()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To avoid unexpected behavior, be cautious when passing functions that use &lt;code&gt;this&lt;/code&gt; keyword. Errors can occur if &lt;code&gt;this&lt;/code&gt; context is not properly bound to the object it needs to reference. Binding &lt;code&gt;this&lt;/code&gt; to the object before passing it around ensures that it always has the expected context.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Fun Fact&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The bound function created by &lt;code&gt;bind()&lt;/code&gt; can be further bound. Although no matter how many times it is rebounded, &lt;code&gt;this&lt;/code&gt; would always point to the initial object it bounded to. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#transforming_methods_to_utility_functions:~:text=A%20bound%20function,by%20boundFn2." rel="noopener noreferrer"&gt;You can read more on that here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; In Arrow Functions
&lt;/h3&gt;

&lt;p&gt;ES6 JavaScript introduced a new type of function called "Arrow Function", which can be written as &lt;code&gt;let func = () =&amp;gt; {}&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;playing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You've probably heard of this statement "Arrow functions don't have their own &lt;code&gt;this&lt;/code&gt;". You may wonder, "What does this mean?".&lt;/p&gt;

&lt;p&gt;Arrow functions don't follow the rules their sibling(i.e normal functions) follows, whereby &lt;code&gt;this&lt;/code&gt; value is determined by how the function is called. In arrow functions, &lt;code&gt;this&lt;/code&gt; refers to the value of its surrounding lexical scope and if none, it defaults to the global object. &lt;code&gt;this&lt;/code&gt; in arrow function is set to what it was when the arrow function was made.&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%2Fqwkh4znjiau7d2f7mhf8.jpg" 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%2Fqwkh4znjiau7d2f7mhf8.jpg" alt="Visual representation of how " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Therefore,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If an arrow function is created in the global scope, &lt;code&gt;this&lt;/code&gt; value refers to the global object e.g
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create an arrow function that returns the value of `this`&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrowFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// An object with a `name` property&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Add a method to the object that is set to `arrowFunc`&lt;/span&gt;
&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arrowFunc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// call `newPhone.Method`&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;//global object&lt;/span&gt;

&lt;span class="c1"&gt;// The arrow function is called,&lt;/span&gt;
&lt;span class="c1"&gt;// but since `arrowFunc` was created in the global scope,&lt;/span&gt;
&lt;span class="c1"&gt;// `this` has been binded by default to the global object,&lt;/span&gt;
&lt;span class="c1"&gt;// even though it was called as a method of the `newPhone` object.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If an arrow function is used as an object method, &lt;code&gt;this&lt;/code&gt; refers to the enclosing lexical scope of the arrow function.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;thisValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;arrowFunc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//arrow function as object method&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;thisValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: global object.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;arrowFunc&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: global object.&lt;/span&gt;
&lt;span class="c1"&gt;// In this example, `this` in "arrowFunc"...&lt;/span&gt;
&lt;span class="c1"&gt;// is similar to `this` in "thisValue" property...&lt;/span&gt;
&lt;span class="c1"&gt;// which is the enclosing lexical scope, &lt;/span&gt;
&lt;span class="c1"&gt;// in this case the global object.&lt;/span&gt;

&lt;span class="c1"&gt;// Remember, objects themselves do not have a scope, only functions and blocks in the object are scoped.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If an arrow function is created inside a normal function, &lt;code&gt;this&lt;/code&gt; value is determined by how the normal function is called e.g
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;arrowFuncInFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// "foo" is an arrow function in a normal function&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;arrowFuncInFunction&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: newPhone object.&lt;/span&gt;
&lt;span class="c1"&gt;// The arrow function inside "arrowFuncInFunction" method...&lt;/span&gt;
&lt;span class="c1"&gt;// returns the `this` value of its enclosing lexical scope,&lt;/span&gt;
&lt;span class="c1"&gt;// which would be determined by how "arrowFuncInFunction" is called.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If an arrow function is invoked using &lt;code&gt;call(), apply(), or bind()&lt;/code&gt;, the "thisArg" passed is discarded because the arrow function &lt;code&gt;this&lt;/code&gt; has already been bounded to the &lt;code&gt;this&lt;/code&gt; context of its lexical scope.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create an arrow function that returns the value of `this`&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrowFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// An object with a `name` property&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// use call() to bind `this` to newPhone&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrowFunc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//global object&lt;/span&gt;

&lt;span class="c1"&gt;// This did not work as expected because,&lt;/span&gt;
&lt;span class="c1"&gt;// the "arrowFunc" has automatically bounded `this`...&lt;/span&gt;
&lt;span class="c1"&gt;// to its lexical scope when it was created in the global scope.&lt;/span&gt;
&lt;span class="c1"&gt;// Therefore trying to rebind it using call() would have no effect,&lt;/span&gt;
&lt;span class="c1"&gt;// the output is still the global object.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt; In Object Methods
&lt;/h2&gt;

&lt;p&gt;Objects Methods in simple terms are functions that are found in objects. Let's use the Samsung phone you purchased earlier as an example, the phone is an object that has object methods(i.e functions) like the ability to play games, watch videos, chat with your ex etc.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Did you know?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Object curly braces {} are not enclosing lexical scope. They just happened to be used by JavaScript a long time ago before scoping was introduced. They do not define a new scope, only the methods in an object creates a new scope.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, when using &lt;code&gt;this&lt;/code&gt; with objects properties that aren't methods, &lt;code&gt;this&lt;/code&gt; refers to the global object, while &lt;code&gt;this&lt;/code&gt; in object methods refers to how the object method is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// object properties that aren't methods&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung S23 Ultra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;thisPhone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// object method&lt;/span&gt;
  &lt;span class="nf"&gt;thisPhoneMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;thisPhone&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// global object&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;thisName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// newPhone objet&lt;/span&gt;

&lt;span class="c1"&gt;// "newPhone.thisPhone" property points to the window object...&lt;/span&gt;
&lt;span class="c1"&gt;// because `this` can not be assigned as a value...&lt;/span&gt;
&lt;span class="c1"&gt;// "newPhone.thisPhoneMethod" points to the "newPhone" object...&lt;/span&gt;
&lt;span class="c1"&gt;// because the method was called with the newPhone object.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, objects can use the method of another object as their own method. This can be done by directly storing an object method as a property of the desired object, by prototype inheritance, or by using &lt;code&gt;call, apply and bind&lt;/code&gt; functions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;📍 Note&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Just because a function is created inside an object does not mean the value of &lt;code&gt;this&lt;/code&gt; in that object method would always refer to the object. &lt;code&gt;this&lt;/code&gt; in a particular object method can refer to any object that calls that object method.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; in callbacks
&lt;/h2&gt;

&lt;p&gt;Oh callbacks, a concept in JavaScript that's also a little bit difficult to understand. Well, for those who don't know, callbacks are basically functions that can be called by another function when passed as an argument to that function, hence the name &lt;em&gt;callbacks&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For example, setTimeout accepts a callback function. When setTimeout is called, it automatically invokes the function you passed as an argument at the time specified. Most in-built array methods like map, and forEach all need a callback function as an argument which they execute as soon as they are called.&lt;/p&gt;

&lt;p&gt;In a nutshell, closure gets into a function as an argument, the function invokes the callback when called. That's all.&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;this&lt;/code&gt; in callbacks are relatively simple, they depend on how the callback is called. If you try to attach an object to a callback, &lt;code&gt;this&lt;/code&gt; would refer to the context in which the function that executes the callback is called. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// custom function to execute callback functions&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;invokeCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;human&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eren Yeager&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;getObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;invokeCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;human&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: global object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oof, not what you wanted, right?&lt;/p&gt;

&lt;p&gt;why does this happen? Well, you passed "human.getObject" as a callback to "invokeCallback" function, and you handed over the invocation rights to "invokeCallback". Remember, &lt;code&gt;this&lt;/code&gt; is determined by how a function is called, so since "invokeCallback" was the one that invoked the callback function, &lt;code&gt;this&lt;/code&gt; couldn't find any object to refer to so it defaults to the global object.&lt;/p&gt;

&lt;p&gt;To fix this, you'd have to explicitly bind &lt;code&gt;this&lt;/code&gt; to "human" object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;invokeCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;human&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eren Yeager&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;getObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// explicitly bind `this` to human object&lt;/span&gt;
&lt;span class="c1"&gt;// bind() returns new function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boundHumanObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;human&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;human&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// pass bounded function as callback for "invokeCallback"&lt;/span&gt;
&lt;span class="nf"&gt;invokeCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;boundHumanObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// human object&lt;/span&gt;

&lt;span class="c1"&gt;// works perfectly now.&lt;/span&gt;
&lt;span class="c1"&gt;// No matter what calls the function, `this` would always reference "human" object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some JavaScript inbuilt methods actually allows you to specify the &lt;code&gt;this&lt;/code&gt; value of a callback as an argument. They allow you to pass both the callback and the &lt;code&gt;this&lt;/code&gt; value as arguments. A good example would be the map array method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;human&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jackie Chan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// callback function that set abilities to any object&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;setAbilities&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; can &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// array with abilities for an object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;abilities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fight&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sleep&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Use map() to call "setAbilities" with each item on "abilities" array.&lt;/span&gt;
&lt;span class="c1"&gt;// Pass the "human" object as second argument to map(),&lt;/span&gt;
&lt;span class="c1"&gt;// so that `this` in "setAbilities" refers to the "human" object.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jackieAbilities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;abilities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setAbilities&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;human&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jackieAbilities&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//outputs: ["Jackie Chan can eat", "Jackie Chan can fight", "Jackie Chan can sleep"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other JavaScript in-built methods that accept &lt;code&gt;this&lt;/code&gt; as an argument include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Array methods - every(), filter(), find(), findIndex(), forEach(), map(), reduce(), reduceRight(), some(), apply(), bind(), call(), match(), replace(), search(), split()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function methods - call(), apply() and bind()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TypedArray methods - every(), filter(), forEach(), map(), reduce(), reduceRight(), some()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reflect methods - apply(), construct(), defineProperty(), deleteProperty(), get(), getOwnPropertyDescriptor(), getPrototypeOf(), has(), isExtensible(), ownKeys(), preventExtensions(), set(), setPrototypeOf()&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; In Constructors
&lt;/h2&gt;

&lt;p&gt;In simple terms, constructors are object builders; well, life would have been great if that was all it was, so let's dive in.&lt;/p&gt;

&lt;p&gt;Constructors are functions used to create new objects with common properties and methods. Apart from the arrow function, any function can serve as a constructor by invoking it with the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in constructors would always refer to the new object that the constructor builds when invoked with &lt;code&gt;new&lt;/code&gt; keyword. In a constructor function, &lt;code&gt;this&lt;/code&gt; has no context until the constructor is invoked, at which point it becomes the newly created object.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword is a core feature that's needed by constructors to build objects. You can think of how constructors work like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Phone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// this = {}; (new object created in private by JavaScript)&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// return this; (Javascript returns newly created objects by default.)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// When calling the constructor, &lt;/span&gt;
&lt;span class="c1"&gt;// the 'new' keyword creates a new object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Phone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Samsung&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Note 9&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// The 'new' keyword creates an empty object,&lt;/span&gt;
&lt;span class="c1"&gt;// and set `this` to the object.&lt;/span&gt;
&lt;span class="c1"&gt;// The constructor copies all properties in it to the empty objects&lt;/span&gt;
&lt;span class="c1"&gt;// by using `this` to refer to the new object.&lt;/span&gt;
&lt;span class="c1"&gt;// The new object is then returned from the constructor.&lt;/span&gt;
&lt;span class="c1"&gt;// The new "newPhone" variable now references the new object that was created.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The majority of constructors' names begin with an uppercase letter. It isn't compulsory but just don't be that person.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;🚫 Why arrow functions cannot be used as constructors&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The reason why arrow functions can't be used as a constructor is because &lt;code&gt;this&lt;/code&gt; in arrow functions are auto-bounded to the &lt;code&gt;this&lt;/code&gt; context of its lexical scope when created. This wouldn't work for a constructor since &lt;code&gt;this&lt;/code&gt; needs to be bound by &lt;code&gt;new&lt;/code&gt; keyword to a newly created object which unfortunately arrow functions can't do.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; in class
&lt;/h2&gt;

&lt;p&gt;Classes in JavaScript are like blueprints for building objects.&lt;/p&gt;

&lt;p&gt;Developers often refer to them as synthetic syntax for function constructors because they provide a more concise and readable syntax for defining constructor functions.&lt;/p&gt;

&lt;p&gt;In this article, we're focusing more on how &lt;code&gt;this&lt;/code&gt; works in classes so going into the nitty gritty details of how constructor works won't be possible (It's quite broad). &lt;a href="https://javascript.info/classes" rel="noopener noreferrer"&gt;Take a deep dive into classes here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple declaration of a class compared to a function constructor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Class Syntax&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Phone&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getPhoneName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Function Syntax&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Phone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;Phone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPhoneName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// In this example, `this` works the same way in both syntax.&lt;/span&gt;
&lt;span class="c1"&gt;// Although, the class syntax is much better to look at.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When dealing with &lt;code&gt;this&lt;/code&gt; in classes, there are two contexts you need to watch out for: Instance and Static. &lt;code&gt;this&lt;/code&gt; has different effects in both contexts. I know, it's a lot.&lt;/p&gt;

&lt;h3&gt;
  
  
  Instance context
&lt;/h3&gt;

&lt;p&gt;This refers to the properties and methods found in a class that objects created from that class would inherit. In this context, &lt;code&gt;this&lt;/code&gt; refers to the object the class creates.&lt;/p&gt;

&lt;p&gt;Constructors, instance methods, and instance fields are all part of the instance context.&lt;/p&gt;

&lt;p&gt;Most of the time, you'd use the instance context because they're the ones that contains all what a class needs to create new objects. Now, as complicated as all these may sound, &lt;code&gt;this&lt;/code&gt; is actually pretty straightforward in instance context.&lt;/p&gt;

&lt;p&gt;Instance context constructors and fields use &lt;code&gt;this&lt;/code&gt; to refer to the object generated by the class whereas instance method use "this" to refer to the object that called the method (similar to how &lt;code&gt;this&lt;/code&gt; in object method works).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Game&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getDetails&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is a &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; game.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;COD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shooting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;streetFighter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Street Fighter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fighting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;cod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDetails&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// COD is a shooting game.&lt;/span&gt;
&lt;span class="nx"&gt;streetFighter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDetails&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Street Fighter is a fighting game&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;this&lt;/code&gt; works as expected. When the class gets invoked with 'new' keyword(very important), a new object is created by the class and all properties found in the class constructor would be made as properties for the new object. Also, all instance methods of the class would be inherited by the new object using the &lt;a href="https://javascript.info/prototype-inheritance" rel="noopener noreferrer"&gt;prototype inheritance&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static context
&lt;/h3&gt;

&lt;p&gt;This refers to the properties and methods that's used only by the class or subclasses. In other words, objects created by classes do not have access to information found in the static context. In static context, &lt;code&gt;this&lt;/code&gt; refers to the class itself.&lt;/p&gt;

&lt;p&gt;Static methods, static fields and static initialization blocks are all part of the static context.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;static&lt;/code&gt; keyword is used to declare a method or field as part of the static context. This is a good way to differentiate methods or fields into to their respective context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Game&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// instance context&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getDetails&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is a &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; game.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// static context&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Class for Game objects&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Class for Game objects&lt;/span&gt;

&lt;span class="c1"&gt;// create an object and try to access a static method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;COD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shooting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// error: cod.getName is not a function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code snippet above, when the static method &lt;code&gt;getName()&lt;/code&gt; is called, it returns the value of the static property name since &lt;code&gt;this&lt;/code&gt; refers to the class when in static context.&lt;/p&gt;

&lt;p&gt;However, when we tried to call &lt;code&gt;getName()&lt;/code&gt; using an object created by the class, it fails because &lt;code&gt;getName()&lt;/code&gt; is a static method, which the new object doesn't have access to.&lt;/p&gt;

&lt;p&gt;Keep in mind that objects created by a class cannot access static properties or methods; only the class itself and its subclasses can access them.&lt;/p&gt;

&lt;p&gt;Like I said, classes can get pretty wild if you're just starting, but no worries you learn as you go (hopefully).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;❕ Important&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There's also a popular method that can be found in classes that inherits properties from another class - a method called super().&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;super()&lt;/code&gt; is a method that is passed to a class constructor that inherits properties from another class constructor. &lt;code&gt;this&lt;/code&gt; refers to the object that called super() which is basically the constructor that's about to create a new object.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; In Event Handlers
&lt;/h2&gt;

&lt;p&gt;Events and events handlers are features in JavaScript that makes developers add interactivity to a website and as expected, &lt;code&gt;this&lt;/code&gt; keyword also made its way there. Fortunately, using &lt;code&gt;this&lt;/code&gt; in event handlers is really straightforward and easy to understand.&lt;/p&gt;

&lt;p&gt;Event handlers are functions that are invoked anytime a DOM element trigges an event. The &lt;code&gt;this&lt;/code&gt; keyword in event handlers refer to the DOM element that triggered the event. This makes it incredibly simple for the event handler to access the properties or methods of the precise DOM element that triggered the event.&lt;/p&gt;

&lt;p&gt;A simple code example is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//--- HTML Code ---&lt;/span&gt;
&lt;span class="c1"&gt;// &amp;lt;button id="showButton"&amp;gt;Hi, I show stuffs&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// &amp;lt;button id="hideButton"&amp;gt;Hi, I hide stuffs&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;//--- JavaScript Code ---&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#showButton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#hideButton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//Event handler function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;button1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// output: showButton&lt;/span&gt;
&lt;span class="c1"&gt;// output: Hi, I show stuffs&lt;/span&gt;

&lt;span class="nx"&gt;button2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// output: hideButton&lt;/span&gt;
&lt;span class="c1"&gt;// output: Hi, I hide stuffs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, two event listeners were added to two buttons. The addEventListener is set to call the &lt;code&gt;handleClick&lt;/code&gt; function when a "click" event occurs on any of the buttons. When the event occurs, &lt;code&gt;this&lt;/code&gt; inside the &lt;code&gt;handleClick&lt;/code&gt; function would refer to the button element that triggered the event. The id and innerHTML of the button can then be accessed through &lt;code&gt;this&lt;/code&gt;. Therefore, &lt;code&gt;this&lt;/code&gt; provides an easy way to access information about the specific element that triggered the event.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices When Using &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Over the years, developers have battled with the fickle nature of &lt;code&gt;this&lt;/code&gt; so as to find out why it doesn't work as expected in their code. So here are some helpful tips and tricks that you can use to rule over the "this" keyword:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in functions is determined by how the function is invoked. Alone, this would refer to the global object (non-strict mode) or undefined (strict mode).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be careful when using &lt;code&gt;this&lt;/code&gt; in arrow functions, &lt;code&gt;this&lt;/code&gt; in arrow functions refers to the &lt;code&gt;this&lt;/code&gt; context of the arrow function lexical scope&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in global scope is the global object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in object methods refers to the object that the method is called upon, not the object that owns the method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in event handlers refers to the DOM element the event handler is attached to.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is a keyword; you can access what it refers to but you can't assign it a value like how variables do e.g &lt;code&gt;this = car; // error&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt;, or &lt;code&gt;apply&lt;/code&gt; to explicitly set the &lt;code&gt;this&lt;/code&gt; value to a desired object of choice. However, keep in mind that these methods cannot change &lt;code&gt;this&lt;/code&gt; in arrow functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When using &lt;code&gt;this&lt;/code&gt;, use strict mode to avoid unexpected behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always use &lt;code&gt;new&lt;/code&gt; when calling a constructor function, if you don't &lt;code&gt;this&lt;/code&gt; would refer to the global object.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Wow, it's been a long journey. Thanks for making it to the end! I'm proud of you. If you enjoyed this article, a like would be much appreciated.&lt;/p&gt;

&lt;p&gt;Got any questions? Drop them in the comments section, and I'll do my best to help you out.&lt;/p&gt;

&lt;p&gt;And hey, if you have any topics or ideas you'd like me to explore, I'm all ears! I'm always available to chat and see what we can create together. Don't hesitate&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Send me a text on Twitter - &lt;a href="https://twitter.com/Vayo_tweet" rel="noopener noreferrer"&gt;@vayo_tweet&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Send me an email - &lt;a href="mailto:vayospot@gmail.com"&gt;vayospot@gmail.com&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now go out there and dominate that project. You've got &lt;code&gt;this&lt;/code&gt;!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
      <category>tools</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
