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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:39:44+00:00 2026-06-15T14:39:44+00:00

I’m using multiple prototypes to manage some complex situations and I’m having trouble with

  • 0

I’m using multiple prototypes to manage some complex situations and I’m having trouble with the use of ‘this’. I’m not 100% certain how to describe as I’m not familiar with the correct terminology, but I’ll have a go.

Function X has a variable that points to a function within function Y. This method within function Y wishes to access a variable that is within the scope of the function Y, but when I use this, it refers to function X.

I’ve created a JSFiddle with an example, using prototypes and so on (as that is the scenario that I find myself in). However the code is short, so I’ll include it below:

var Abc = function(options) {
    alert('2');
    options = options || {};
    this.myVar = options.myVar || 'Abc.myVar';
};
Abc.prototype = {
    onEvent: function() {
        alert('myVar: ' + this.myVar);
    }
};
var Xyz = function(options) {
    alert('3');
    options = options || {};
    this.listenFn = options.listenFn || function() {
        alert('x');
    };
    this.myVar = options.myVar || 'Xyz.myVar';
};
Xyz.prototype = {
    execute: function() {
        alert('4');
        this.listenFn();
    }
};
$(document).ready(function(){
    alert('1');
    var a = new Abc();
    var x = new Xyz({listenFn: a.onEvent});

    x.execute();
});

I’ve put in alerts in sequence of the code execution. So:

  1. is the start of the document ready.
  2. is the creation of the Abc instance.
  3. is the creation of the Xyz instance, where the onEvent function for the instance of Abc is assigned to the listenFn for Xyz.
  4. is inside the point where execute on Zyz has been called which in turn calls the onEvent of Xyz and it displays the value for this.myVar.

The problem I have is that ‘this’ is referring to the Xyz instance when inside a method on Abc. I need to be able to set/get the value of myVar for the instance of Xyz. I suspect I could do something messy with creating a function that is passed into the new Xyz statement and thus have the instance of Abc passed back to itself, but unfortunately this is not possible with what I am working with (also not very elegant).

Cheers.

  • 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-15T14:39:45+00:00Added an answer on June 15, 2026 at 2:39 pm

    EDIT

    Here’s the answer to your question

    $(document).ready(function(){
        alert('1');
        var a = new Abc();
        var x = new Xyz({listenFn: a.onEvent.bind(a) }); // <------- bind
    
        x.execute();
    });
    

    Using bind like this will pre-set the this value inside of a.onEvent to be a. Just note that IE8 does not support bind, but you can easily add a shim from here.

    The other way to do this would be to say:

    $(document).ready(function(){
        alert('1');
        var a = new Abc();
        var x = new Xyz({listenFn: function() { a.onEvent() }); 
    
        x.execute();
    });
    

    Without diving too deeply into your code, I’ll just say this: figuring out the value of this in JavaScript is quite easy; it all depends on the manner in which the function is called.

    • If you have x.foo(), then, no matter what*, this will be equal to x inside of foo.

    • If you have foo(), then, no matter what*, this will be the global object, or undefined in strict mode inside of foo.

    • If you say new foo() then this will be a new object whose prototype will be set to foo.prototype

    • If you say foo.call(a), there this will be a* inside of foo, but that doesn’t really apply here.

    *The one exception being that if you had declared foo as

    var foo = function() { }.bind(obj);
    

    then you’ve pre-bound this to be obj for all calls of foo, and will override the rules above.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the

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.