This is a JavaScript keyword. There are many articles on the web that explain this. Here is a reflection on this: what is this and why is this.

What is thethis?

This is an attribute in the record describing the execution of the function. It can only be accessed inside the function and is used during the execution of the function.

See the specification for the abstract operation Call(F, V [, argumentsList]) :

The abstract operation Call is used to call the [[Call]] internal method of a function object. The operation is called with arguments F, V, and optionally argumentsList where F is the function object, V is an ECMAScript language value that is the this value of the [[Call]], and argumentsList is the value passed to the corresponding argument of the internal method. If argumentsList is not present, a new empty List is used as its value.

Function Calls (); Function Calls ();

. 4.Let ref be the result of evaluating memberExpr. ... 9.Return ? EvaluateCall(func, ref, arguments, tailCall).Copy the code

Go to the EvaluateCall:

1.If Type(ref) is Reference, then
    a. If IsPropertyReference(ref) is true, then
        i. Let thisValue be GetThisValue(ref).
    b. Else the base of ref is an Environment Record,
        i. Let refEnv be GetBase(ref).
       ii. Let thisValue be refEnv.WithBaseObject().
2.Else Type(ref) is not Reference,
    a. Let thisValue be undefined.
Copy the code

EvaluateCall does not have this as an input parameter. In this case, it makes sense that this is a property of an internal procedure during function execution. This is an attribute in the record describing the execution of the function and can only be accessed inside the function

Why are therethis?

Why is there, can you think about the use of this, what problem does it solve?

You Don’t Know JS: this & Object Prototypes

This provides a more elegant way to implicitly “pass” an object reference, so the API can be designed to be cleaner and easier to reuse.

function identify() {
  return this.name.toUpperCase();
}

function speak() {
 var greeting = "Hello, I'm " + identify.call(this);
}

var me = { name: "Kyle" };
var you = { name: "Reader" };

identify.call(me); // KYLE
identify.call(you); // READER

speak.call(me); // Hello, I'm KYLE
speak.call(you); // Hello, I'm READER
Copy the code

(it’s even… Try to think of it in terms of the word itself. This should be used as a pronoun. It should be used as a substitute or indicative. What does it refer to? That brings us back to the ECMAScript specification:

The base value component is either undefined, an Object, a Boolean, a String, a Symbol, a Number, or an Environment Record. A base value component of undefined indicates that the Reference could not be resolved to a binding. The referenced name component is a String or Symbol value.

So there’s a lot of possibilities for what this refers to. If undefined, an Object, a Boolean, a String, a Symbol, a Number, etc., use an identifier instead of this.

This is an environment record, a nearby environment record.

The resources

  1. You Don’t Know JS: this & Object Prototypes
  2. What is “this” keyword in JavaScript?
  3. How does the “this” keyword work?
  4. JavaScript digs into this from the ECMAScript specification
  5. JS this
  6. What is the value of this? Once and for all