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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:13:11+00:00 2026-06-06T09:13:11+00:00

Usually (if not always), when jQuery allows you to add a callback to some

  • 0

Usually (if not always), when jQuery allows you to add a callback to some JS event like click, in the callback function they change the “meaning” of this into the DOM element which triggered the event.

This can be quite useful, but it will stand in your way when you write OOP code in js, like in this example:

function MyClass() {}

MyClass.prototype = {

    init: function() {
        $("#someSpan").click(this.doSomething);
    },

    doSomething: function() {
        alert("Here 1");
        this.test();
        return false;
    },

    test: function() {
        alert("Here 2");
    }
}

In this example, this.test() will not work, because this is not anymore an instance on MyClass but instead a jQuery DOM element (the span).

My questions are: is there a way to continue writing OOP code in JS using this pattern and also use jQuery? And: why is jQuery changing this in the callback function when it could as easily send the jQuery DOM element as first argument ?

  • 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-06T09:13:12+00:00Added an answer on June 6, 2026 at 9:13 am

    jQuery has $.proxy that can be used like so:

    function MyClass() {
        this.clicked = $.proxy(this.clicked, this);
    }
    
    MyClass.prototype = {
    
        clicked: function(e) {
            alert("Here 1");
            this.test();
            e.currentTarget; //this replaces "this"-the keyword used in "non OOP" contexts
    //see http://api.jquery.com/event.currentTarget/
        },
    
        init: function() {
            $("#someSpan").click(this.clicked);
        },
    
        test: function() {
            alert("Here 2");
        }
    }
    

    When you create an instance, that instance gets its own .clicked function that shadows the generic one in the prototype. It will
    always have same this binding no matter how you call it. So you can pass this.clicked all over the place and have it work.

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

Sidebar

Related Questions

Using jQuery on() version 1.7. I bind my events usually like this: $(.foo).on(click, function()
They usually involve generics. But some methods with generics don't have them, and not
I may not always understand every function in an API but usually I can
Usually it will ignore C files that are not documented, but I'd like to
I have an input PDF file (usually, but not always generated by pdfTeX), which
I have read in a lot of places that assembly language is not usually
Usually JAXB works but it is not parsing my schema correctly or something as
I usually try to do TDD with not much analysis (no diagrams) before start
So I usually run my programs through Eclipse but I'm not allowed to use
Usually I was doing something like that (just a example): using (Stream xmlStream =

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.