Skip to content

Latest commit

 

History

History
46 lines (26 loc) · 1.51 KB

File metadata and controls

46 lines (26 loc) · 1.51 KB

this

{% embed url="https://dev.to/naveenkolambage/test-11fd" %}

What does 'this' return?

Here's an object which uses this keyword

const man = {
  name: "rick",
  adventure() {
    console.log(this);
  }
};

man.adventure(); 

Executing above you will see the man object on the console.

Alt Text

But what if you do;

const adventure_reference = man.adventure;

adventure_reference();

Output then would be;

Alt Text

Explanation

Value of 'this' is determined by how a function is called;

  • If we call a function as a method in an object this will always return a reference to that object.
  • If we call a function as a standalone object - or outside an object this will return the global object which is the window object in browsers.