Context - TristanVarewijck/Block-Tech GitHub Wiki

What is Javascript context?

"Context is related to objects. It refers to the object to which a function belongs. When you use the JavaScript โ€œthisโ€ keyword, it refers to the object to which function belongs"

This

In most cases the value of this is determined by the way a 'function' is called. It can't be set by assignment during execution, and it may be different each time the function is called.

Code example:

const test = {
  prop: 42,
  func: function() {
    return this.prop;
  },
};

console.log(test.func());
// expected output: 42

De context van het object prop: die de function gebruikt is de key value 42.

Global Context

In the global execution context (outside of any function), this refers to the global object whether in strict mode or not.

The window object also refers to the global context in web browsers that why console.log(window === this) // true You can check the context for example global variables by running this or window in the browser.

myName = 'Tristan' 

console.log(window.myName) // Tristan 
console.log(this.myName) // Tristan

This is possible because its globally assigned

Function Context

"Inside a function, the value of this depends on how the function is called."

let a = 20;

function gx () {
    return this;
}

function fx () {
    return this.a;
}

function fy () {
    return window.a;
}

console.log(gx() === window);
// => True
console.log(fx());
// => 20
console.log(fy());
// => 20

De context van de functions is op te halen met zowel window als met this de value die word gelogged is de context waarmee de function is gemaakt / aangeroepen.

Conclusie

Ik begrijp de context nog niet helemaal want heb ook geen idee wat je er dan dus mee kan bereiken ik ga hier zeker verder onderzoek naar doen maar voor nu laat ik dit even bezinken voordat ik verder ga.

Resource