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

You must login to ask a question.

Forgot Password?

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8209059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:35:24+00:00 2026-06-07T09:35:24+00:00

I have problem which still bothers me on js oop – I’m sure I’m

  • 0

I have problem which still bothers me on js oop – I’m sure I’m doing it bad, but I cant get how to do it right.

For example, I have this code

Auth.prototype.auth = function () {
    var request = new XMLHttpRequest();

    request.open('GET', this.getAuthServerURL() + '/token', true);
    request.send();

    request.onloadend = function () {
      var response = JSON.parse(request.responseText);

      console.log(response);
      if(response.result == 'found') {
        var token = response.token;

        this.setToken(token);
        this.isSigned = true;
      } else {
        console.log('Not logged yet.');
      }
    }
}

The problem is that I cant access to function setToken from context of “request.onloadend” function – its probably because I lost reference to “this”.

Whats a solution of this problem? Can I somehow pass the “this” var to context of this function?

Thanks!

  • 1 1 Answer
  • 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

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:35:27+00:00Added an answer on June 7, 2026 at 9:35 am

    There are a couple of ways to do this. The most direct is to simply save a copy of the value you need:

    Auth.prototype.auth = function () {
        var request = new XMLHttpRequest();
        var self = this; // save "this" value
    
        request.open('GET', this.getAuthServerURL() + '/token', true);
        request.send();
    
        request.onloadend = function () {
          var response = JSON.parse(request.responseText);
    
          console.log(response);
          if(response.result == 'found') {
            var token = response.token;
    
            self.setToken(token); // use saved "this" value
            self.isSigned = true;
          } else {
            console.log('Not logged yet.');
          }
        }
    }
    

    Another way is to use bind:

    request.onloadend = (function () {
      var response = JSON.parse(request.responseText);
    
      console.log(response);
      if(response.result == 'found') {
        var token = response.token;
    
        this.setToken(token); // use saved "this" value
        this.isSigned = true;
      } else {
        console.log('Not logged yet.');
      }
    }).bind(this);
    

    The second approach is “cleaner”, but it has browser compatibility issues (IE < 9 does not support it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem which I cant seem to find answer to through searches
My problem is, that I have got a TabActivity, which has 4 Tabs right
Im doing some javascript code and im using firefox. i have a problem which
I have a problem which is most likely a simple problem, but neverthe less
I have a problem which I will try to reproduce: 1)I get a SSH
I have a problem which I don't really know how to solve. I have
I have a problem which makes me crazy. I think it is very easy
I have a problem which requires a reversable 1:1 mapping of keys to values.
I have a problem which I don't know how to fix. It has to
I have a problem which i can't seem to find the solution to. I

Explore

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

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.