11

A Helpful Algorithm to Determine "this" value in JavaScript

 3 years ago
source link: https://dmitripavlutin.com/javascript-this-algorithm/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A Helpful Algorithm to Determine "this" value in JavaScript

Every JavaScript developer, including myself, has been struggling in understanding how this keyword works.

I’ve created a universal algorithm to help you determine the value of this keyword in any situation.

While I made the algorithm as accessible as possible, I recommend reading it multiple times and understand the related terms.

Also, I’ll show you a step-by-step evaluation of the algorithm for example situations. Finally, try the homework exercises by yourself!

Note: If you don’t understand the algorithm from the first time: that’s expected! Get back to the post later and try again until you crack it.

Ready? Let’s begin!

1. this algorithm

The formal definition of ThisValueOfFunction(func, invocationType) that returns this value a function func invoked in a certain way invocationType.

ThisValueOfFunction(func, invocationType):

  1. If func is an arrow function, then

    1. If func is defined in the outermost scope, then return globalObject
      1. let outerFunc be the outer function of func
      2. return ThisValueOfFunction(outerFunc, outerInvocationType)
  2. Else if func is a bound function of an originFunc function, then

    1. let thisArg be the argument of func = originFunc.bind(thisArg)
    2. return thisArg
  3. Else if func is a constructor() method inside of a class SomeClass, then

    1. let instance be the instance of the class instance = new SomeClass()
    2. return instance
  4. Else if func is a regular function, then

    1. If invocationType is as a constructor, then

      1. let newObject be the newly constructed object newObject = new func()
      2. return newObject
    2. Else if invocationType is indirectly, then

      1. let thisArg be the argument of func.call(thisArg) or func.apply(thisArg)
      2. return thisArg
    3. Else if invocationType is as a method, then

      1. let object be the object upon which func is invoked on object.func()
      2. return object
    4. Else if invocationType is regular, then

      1. If strict mode is enabled, then return undefined
      2. Else return globalObject

1.1 The terms used in the algorithm

The algorithm uses plenty of JavaScript terms. If you aren’t familiar with something, expand and look at the explanation.

Arrow functionBound functionRegular functionconstructor()Outermost scopeOuter functionGlobal objectInvocationConstructor invocationIndirect invocationMethod invocationRegular invocationStrict mode

2. Examples

Example 1

const myFunc = () => {
  console.log(this); // logs `window`};

myFunc();

Try the demo.

ThisValueOfFunction(myFunc, “regular”)

myFunc is an arrow function: thus matching the point 1 in the algorithm. Also myFunc is defined in the outermost scope, matching the point 1.1.

The point 1.1 of the algorithm says return globalObject: meaning that this value inside myFunc is the global object — window (in a browser environment).

Example 2

const object = {
  method() {
    console.log(this); // logs { method() {...} }  } 
};

object.method();

Try the demo.

ThisValueOfFunction(object.method, “as a method”)

method(), while being a property of the object, is a regular function. The point 4 of the algorithm is matched.

object.method() is a method invocation because of the property accessor usage: thus the point 4.3 is matched.

Then, according to point 4.3, this value inside method() equals the owning object of the method invocation (object.method()) — object.

Example 3

function MyCat(name) {
  this.name = name;

  const getName = () => {
    console.log(this); // logs { name: 'Flufyy', getName() {...} }    return this.name;
  }

  this.getName = getName;
}

const fluffy = new MyCat('Fluffy');
fluffy.getName();

Try the demo.

ThisValueOfFunction(getName, “as a method”)

getName() is an arrow function, thus the point 1 of the algorithm is applied. Then the point 1.2 matches, because MyCat is the outer function of getName().

The point 1.2.2 says that this value inside getName() arrow function equals this value of the outer function: MyCat.

So, let’s run the algorithm recursively again on MyCat function — ThisValueOfFunction(MyCat, "as a constructor").

ThisValueOfFunction(MyCat, “as a constructor”)

MyCat is a regular function, thus the point 4 of the algorithm is applied.

Because MyCat was invoked as a constructor new MyCat('Fluffy'), the point 4.1 is applied. Finally, according to points 4.1.1 and 4.1.2, this value inside MyCat equals to the constructed object: fluffy.

And, returning back to the arrow function’s point 1.2.2, this inside of the getName() equals this of the MyCat — which is finally fluffy.

3. Homework

The best way to understand the algorithm is by trying it yourself. Follow the 3 exercises in determining this value.

Exercise 1

const myRegularFunc = function() {
  console.log(this); // logs ???};

myRegularFunc();

How would the algorithm determine this value inside myRegularFunc()? Write the step-by-step evaluation.

Exercise 2

class MyCat {
  constructor(name) {
    this.name = name;
    console.log(this); // logs ???  }
}

const myCat = new MyCat('Lucy');

How would the algorithm determine this value inside new MyCat('Lucy')? Write the step-by-step evaluation.

Exercise 3

const object = {
  name: 'Batman',

  getName() {
    const arrow = () => {
      console.log(this); // logs ???      return this.name;
    };

    return arrow();
  };
}

object.getName();

How would the algorithm determine this value inside arrow() function? Write the step-by-step evaluation.

4. Summary

In this post, I presented a universal algorithm to determine the value of this inside of an invoked function.

While the algorithm might be challenging at first, if you understand the step-by-step examples, you will realize how easy is to apply the algorithm.

Struggle applying the algorithm for a certain situation? Describe your case in a comment below!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK