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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:30:13+00:00 2026-05-15T05:30:13+00:00

The following code doesn’t work as I intuitively expect it to: function MyObject(input) {

  • 0

The following code doesn’t work as I intuitively expect it to:

function MyObject(input) {
   input.change(this._foo);
   this.X = undefined;
}

MyObject.prototype._foo = function() {
   alert("This code is never called");
   // but if it did
   this.X = true;
}

var test_input = $("input#xyz"); // a random, existing input

var m = MyObject(test_input); // attach handler (or try to)

test_input.change(); // trigger event

alert(m.X); // undefined

I’d expect that _foo() would be called (and, if that ever happens, that the this variable in _foo() would be an instantiation of MyObject.

Does anyone know why this doesn’t work, and of any alternative pattern for passing an object to an event handler?

Thank you for reading.

Brian

  • 1 1 Answer
  • 4 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-15T05:30:14+00:00Added an answer on May 15, 2026 at 5:30 am

    As Kenny points out you’re missing the new. You also need to make sure that this in _foo refers to the MyObject instance
    One way to do it:-

    function MyObject( input ) {
        var _this = this;
        input.change( function() {
           // explicitly set the `this` in _foo to `_this`
            _this._foo.call( _this );
        });
       this.X = undefined;
    }
    
    MyObject.prototype._foo = function( event ) {
       alert("This is called");
       // and 'this', being 'm', has X set to true
       this.X = true;
       // the textbox must be accessed by 'event.target' not 'this' if you need it
    }
    
    var test_input = jQuery("input#xyz"); // a random, existing input
    
    var m = new MyObject(test_input); // attach handler (or try to)
    
    test_input.change(); // trigger event
    
    alert(m.X); // true
    

    P.S
    You can’t avoid using the new operator by leaving it out! 🙂

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

Sidebar

Related Questions

following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666
The following code doesn't work! 'this' in in the context when i access it
The last log in the following code doesn't work in Firefox. Why? (function() {
The following code doesn't work. How does Facebook do this? Is there any way
The following code doesn't work. $(document).ready(function(){ $(#digitalchange).validate( { rules: { addbalance: { digits:true, min:20,
Why the following code doesn't work? I want that input would be disabled for
dose php5 forbid this usage, the following code doesn't work class Foo{ public static
Can anyone explain to me why the following code doesn't work within a function
Why the following code doesn't work? <html> <head> <script type=text/javascript> function addTable() { var
Why the following code doesn't work? (if I change remote source there to local,

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.