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

  • Home
  • SEARCH
  • 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 9115605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:28:21+00:00 2026-06-17T04:28:21+00:00

Short description: I am using OO Javascript with the function declaration, new keyword, and

  • 0

Short description: I am using OO Javascript with the function declaration, new keyword, and prototype approach (example below). I need a way to reference the “self” object within each method of the object. “this” seems to only work if I am calling the method directly, otherwise “this” seems to refer to whatever called the method instead.

More details: Here is a simplified version of my object.

function Peer(id) {
    this.id = id;
    this.pc = new RTCPeerConnection(SERVER);

    this.pc.onevent1 = this.onEvent1;
    this.pc.onevent2 = this.onEvent2;
}

Peer.prototype.doSomething = function() {
    // create offer takes one param, a callback function
    this.pc.createOffer(this.myCallback);
};

Peer.prototype.onEvent1 = function(evt) {
    // here this refers to the RTCPeerConnection object, so this doesn't work
    this.initSomething(evt.data);    
};

Peer.prototype.myCallback = function(data) {
    // here this refers to the window object, so this doesn't work
    this.setSomething = data;
};

Peer.prototype.initSomething = function(data) {
    // use data
};

And here is a sample use of it.

var peer = new Peer(1);
peer.doSomething();
// eventually something triggers event1

I tried to simplify the code as much as possible and explain the problem in comments. I created a workaround for the first scenario (calling this.myCallback) by creating a local copy of this and making the callback param an anonymous function that calls the function I need using my local copy of this. But the second scenario is more troublesome (an event firing).

I was curious if there is a different model for Object Oriented programming in which every method always has a proper reference to the parent object, regardless of how the method was invoked? Or if there is a different way to create an object as a property of a custom object and bind its events to the custom object’s method? Sorry if this is confusing language! Note that my question has nothing to do with RTCPeerConnection, that just happened to be the project I’m working on currently.

I found this article: http://www.alistapart.com/articles/getoutbindingsituations but I wasn’t totally sure how to use this information?

  • 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-17T04:28:22+00:00Added an answer on June 17, 2026 at 4:28 am

    I was curious if there is a different model for Object Oriented programming in which every method always has a proper reference to the parent object, regardless of how the method was invoked?

    Yes, there is: it’s called Function.bind.

    function Peer(id) {
        this.id = id;
        this.pc = new RTCPeerConnection(SERVER);
    
        this.pc.onevent1 = this.onEvent1.bind(this);
        this.pc.onevent2 = this.onEvent2.bind(this);
    }
    

    Likewise, for myCallback‘s this to always reference the Peer instance:

    Peer.prototype.doSomething = function() {
        // create offer takes one param, a callback function
        this.pc.createOffer(this.myCallback.bind(this));
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on one application ( using PHP, javascript ). Below is the short
Short Description I am using charts for a specific application where i need to
I have a table with films. There is a field with short description. Using
I need to display short description like label and near it data associated with
short description I'm using jquery, jqueryUI and ajax. I use val() to get value
Short Description The question is not meant to ask if using virtual environments are
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without
Firstly a short description of what I have Images of the same size and
I have a search result like 1. my title my short description...... http://www.stackoverflow.com/tags/thisthat/againthisthat/againandagainthisthat/mypage.html http://www.stackoverflow.com/tags/mypage.html?a=123123123&b=2342343
I read the book Algorithm Design, chapter 1, it gave a very short description

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.