Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5291

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T07:53:07+00:00 2024-11-27T07:53:07+00:00

Relax, JS `this` is damn easy.

  • 61k

Once you know the concept then you would say the same. Yes. There are several cases around this. Hence by knowing all those scenarios you will be cleared. We are generally afraid out of imagination. Let's not simply imagine instead do some practice.

Hence let's look into the scenarios where and when we should ideally use this and what its importance in its scope with different possible examples.

So, basically what this actually refers to in Javascript?

It refers to an object in javascript. Based on the usage of this its reference will be changed. So, if you got all those references then you are done.

I hope this introduction would be fine. OK. Let's decipher this now:

Before entering into explanation I want to give a summary here:

S.No. Scenario `this` refers to
1. Inside object Refers to parent object only
2. Inside browser window Refers to the window object
3. Strict mode in the browser window Refers to the window object
4. Inside function Refers to the window object
5. strict mode: Inside function Returns undefined
6. In Event handler Refers HTML Element
7. Using with call(), bind() and apply() Refers to objects sent as arguments
8. Inside JS classes Refers to the current object.
9. In arrow function Refers to the window object.

1. Inside object

Refers to parent object only.

Example:

const testObj = {     firstName: 'John',     lastName: 'Carter',     name: function name() {         return this.firstName + " " + this.lastName; // Here this refers to `testObj`     } }  console.log(testObj.name()); > John Carter 
Enter fullscreen mode Exit fullscreen mode

2. Alone using this

1. Inside browser window

Refers to the window object.

Hence window methods like alert, atob, blur etc. are accessed using this.

Example:

image.png

Access thealert function of the window object using this.

Example:

image.png

2. Strict mode in the browser window

Also this refers to the window object.

Example:

image.png

3. Inside function

Also this refers to the window object

Example:

image.png

Using strict mode returns undefined.

function thisInsideFunction() {     'use strict'; // see strict mode     return this;   }   console.log(thisInsideFunction()); > undefined 
Enter fullscreen mode Exit fullscreen mode

4. In Event handler

Users can trigger click, enter, keydown events on various form elements in HTML from the browser. While handling those events we bind those event handlers with functions generally. If we pass this from the handler then it refers to the corresponding HTML element object.

Example:

<button onclick="buttonClick(this)">Submit</button>  // this refers to button HTMLElement here. 
Enter fullscreen mode Exit fullscreen mode

5. Using with call(), bind() and apply() methods

These are javascript built-in methods. call() & apply() used to call object method of other object using another object as an argument.

Complete article on these methods are here

Whereas bind borrows object method of another object and creates a new object altogether.

Essentially, all these three things will do a similar kind of work.

now, what this refers to in these methods. It refers to objects sent as arguments.

Example

const testObj = {     firstName: 'John',     lastName: 'Carter',     name: function name() {         return this.firstName + " " + this.lastName;     } }  const anotherObj = {    firstName: 'new john',    lastName: 'Carter' }  console.log(testObj.name.apply(anotherObj)); // this refers to anotherObj > new john Carter 
Enter fullscreen mode Exit fullscreen mode

Same in case of call & bind as well.

6. Inside JS classes

this refers to the current object.

Example

class TestThis {     a;     b;      normalFunction() {         console.log('normal function');         console.log(this);     }      static staticFunction() {         console.log('Static function');     } }  const test = new TestThis(); test.normalFunction();  
Enter fullscreen mode Exit fullscreen mode

image.png

Note that static properties are not part of this.

7. this in arrow function

Refers to the window object.

image.png

inside object method refers to the parent obj.

let testObj = {     test: 10,     bar: function() {       let x = (() => this);       return x;     }   };   console.log(testObj.bar()()); 
Enter fullscreen mode Exit fullscreen mode

I covered most possible cases of this. Please let me know by commenting if you come across any new scenarios.

Also, do comment if you don't understand any scenario.

beginnersjavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.