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 3946514
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:08:32+00:00 2026-05-20T01:08:32+00:00

I admit that I’ve already asked a question about the this keyword and what

  • 0

I admit that I’ve already asked a question about the this keyword and what it means in certain situations, but I still do not completely understand it I’m afraid. Any hints would be greatly appreciated.

I basically have a Socket class, which is a wrapper to the WebSocket class of HTML5 to make things easier (I only copied what is relevant to my question):

var Socket = function(url) {
    this.url = url;
    this.webSocket = null;
}

Socket.prototype.init = function() {
    this.webSocket = new WebSocket(this.url);
    this.webSocket.onmessage = this.parseMessage;
};

Socket.prototype.parseMessage = function(event) {
    console.log(this); // logs the WebSocket (native) instance
}

In the parseMessage function, this refers to the instance of the native WebSocket object, so how can I access the instance to my Socket object here?

Thanks a lot.

  • 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-05-20T01:08:33+00:00Added an answer on May 20, 2026 at 1:08 am

    You can bind it:

    this.webSocket.onmessage = this.parseMessage.bind(this);
    

    However, not all browsers support this natively, but you can grab a code snippet from MDC to make all browsers compatible.

    In you’re unfamiliar with binding … it lets you specify specifically what this should be when that function is called. At the time that line runs, this is your Socket class instance, so you’re basically forcing this inside parseMessage to point back to the instance.

    If, for some reason, you don’t want to use bind, another option is to save a copy of this to a variable so the meaning is not lost inside an event dispatch:

    Socket.prototype.init = function() {
        this.webSocket = new WebSocket(this.url);
        var _self = this;
        this.webSocket.onmessage = function (event) {
            _self.parseMessage.apply(_self, [event]);
        };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I admit that I asked a question about why Closure Compiler does not shorten
So I'll admit that this is a homework assignment, but I'm not asking you
I admit that this question is subjective but I am interested in the view
I'm not afraid to admit that I'm somewhat of a C++ newbie, so this
I have to admit that having not even tried to code this myself this
ok, i have to admit that until now i still do not know the
I will admit that there is already a question exactly along these lines here
I'll be the first to admit that PHP is not my forte and this
I must admit that I don't really know if this is the right place
I've a c++ project. I admit that I'm a complete ZERO in c++. But

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.