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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:16:04+00:00 2026-06-06T12:16:04+00:00

I have been fiddling with code to call a function with the name of

  • 0

I have been fiddling with code to call a function with the name of the value of a variable and then keep the this scope when called, but the this pointer seems to be in context of the element I have used jQuery’s bind method on, rather than the object the function I might be calling is within. To clarify here´s some code to illustrate the problem:

classname.prototype = {
    bindElementToFunction: function(element, functionToCall){
        $(element).bind("click", 
                        {realThis: this, functionToCall: functionToCall}, 
                        this.callFunction);
    },

    // I had hoped I could change the this pointer back to the object by running 
    // it through this function, I have tried using .apply and .call but I can't 
    // seem to get them to work with function pointers
    callFunction: function(event){
        var realThis = event.data.realThis;
        var functionToCall = event.data.functionToCall;
        functionToCall = realThis[functionToCall];
        // I have tried .apply and .call in several different ways but can't seem 
        // to get them to work in this context
        functionToCall(); 
    },
    abitraryFunction: function(){
        this.test();
    },
};

The problem here is then that everything works fine up until abitraryFunction where this is still referring to the element from the bind function. I have tried doing .apply() with the appropriate this pointers, but they do not seem to work.

So here’s the question how do I change the context of the “this” pointer in combination with function pointers?
Feel free to scrap all the code I have written, as long as I am able to do a bind function to an element that then runs a method within a object where “this” is refferring to the object the method is within.

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-06T12:16:06+00:00Added an answer on June 6, 2026 at 12:16 pm

    I think the jQuery bind is making your code way more complicated than it needs to be. The JavaScript bind() function works perfectly:

    http://jsfiddle.net/bQGWS/

    By simply assigning a function to the onclick (or any other event hook) of an element, this is evaluated from the element’s point of view and so points to the element itself.

    When you use bind, you end up with a copy of the function where this is effectively replaced with the var you passed into bind().

    classname = function(){}
    
    classname.prototype = {
        method: function(){
            try {
                alert( this.othermethod() );
            } catch(e) {
                // Method doesn't exist in scope
                alert( 'Wrong scope :(');
            }
        },
    
        othermethod: function(){
            return 'hello desired scope!';
        },
    
        referenceToElement: function(elementId, functionname){
            var el = document.getElementById(elementId);
    
            // Just assigning the function as is
            el.onclick = this[functionname];
        },
    
        bindToElement: function(elementId, functionname){
            var el = document.getElementById(elementId);
    
            // Using the bind function to create a copy in the
            // scope of this (within the prototype)
            el.onclick = this[functionname].bind(this);
        }
    }
    
    var instance = new classname();
    instance.referenceToElement('reference', 'method');
    instance.bindToElement('bound', 'method');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this, and i've been fiddling around with it for a while but
I Have been fiddling with this for hours. I can not get my text
I have been fiddling with this for almost an hour and am getting nowhere.
I'm fiddling with LINQPad and I'm sure this question has been asked but I've
I have been fiddling around with server side interceptors on CXF. But is seems
I have been fiddling with a few lines of code for two days now
Have been working on this question for a couple hours and have come close
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
Have been searching how to convert a dictionary to a string. But the results
I have been fiddling around with settings for a while, and cannot seem to

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.