Happy Middle of the Week!

It’s a lovely day today—sunny with mild temperatures in the high 70s and not too windy. I enjoy Spring, even though the weather can be quite unpredictable during this season.

I’m feeling good overall, but a bit confused at the moment. I’m trying to determine whether I have therapy scheduled for today. Yesterday, I received an email stating that my therapy session was canceled, followed by another email scheduling me for next week.

So, I’m unsure about today’s appointment. I suppose I’ll find out when it’s time for my session, and if I don’t receive an email reminder. While I could reach out to my therapist, I don’t feel inclined to do so. If I don’t have therapy today, that’s cool; I have an appointment scheduled for next Wednesday. It’s possible something unforeseen came up that led to today’s cancellation.

I found some tortillas in the pantry that seemed appealing, so I decided to warm one up and add a bit of butter. However, when I took a bite, it had a moldy taste, prompting me to spit it out. To cleanse my palate, I grabbed a banana, which ended up being my lunch. After that experience, I had lost my appetite, so I ended up throwing away the tortillas.

It’s 1 PM, and it appears that I was correct—the therapy appointment has been canceled. This gives me the opportunity to continue with my coding. I don’t have much to share today; after yesterday’s lengthy post, I find myself struggling to find inspiration. My attention is primarily on coding today. The ‘mybind()’ function is quite perplexing, but I think I’m starting to grasp it. Google has been quite helpful in this process.

Ok, I have the code for the mybind() function:

Function.prototype.myBind = function (context, ...args) {
  const originalFn = this;

  return function (...laterArgs) {
    return originalFn.apply(context, [...args, ...laterArgs]);
  };
};

LINE 1:

Function.prototype.myBind = function (context, ...args) {
  • You’re adding a method to all functions by putting it on Function.prototype.
  • So now any function can call .myBind().

Inside this function:

  • this refers to the function you’re binding (e.g. greet, sayHello, etc).
  • context is the object you want this to refer to when the function runs.
  • …args are any initial arguments you want to “lock in” ahead of time.

LINE 2:

  const originalFn = this;
  • Saves a reference to the function you’re binding — the one .myBind() was called on.
  • We do this because inside the function we’re returning, this will no longer point to it (we’re entering a new scope).

LINE 3:

  return function (...laterArgs) {
  • This returns a new function — this is your “bound” version.
  • You’ll call this later, and it can take additional arguments (laterArgs).

LINE 4:

    return originalFn.apply(context, [...args, ...laterArgs]);
  • originalFn is the original function we saved.
  • .apply(context, argsArray) calls that function and sets its this to the specified context.

We combine the arguments:

  • …args → from when you called .myBind()
  • …laterArgs → from when you called the new bound function
  • So all arguments get passed to the function in order.

It’s now 5 PM, and my mind feels a bit foggy. I hope I was able to explain the mybind() function clearly enough. Honestly, it’s not information that anyone necessarily needs, nor do I believe it will be particularly helpful to others. I mainly do this for my own understanding and clarity. I’m going to prepare myself a cup of tea, as it’s a bit too late for coffee.

Facebooktwitterredditpinterestlinkedinmail