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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:52:50+00:00 2026-06-11T18:52:50+00:00

This is a basics question but I cannot figure out why the context (

  • 0

This is a basics question but I cannot figure out why the context (this) is correct in the second event handler and incorrect in the first one.

I have this simple constructor function to create the object myNotifier:

function Notifier ( message ) {
  this.message = message;
  this.saySomething = function () {
    alert( "I say:" + this.message);
  }
}
   
myNotifier = new Notifier(" HELLO!");  

Then I use the myNotifier.saySomething() method as an event handler for CLICK on two buttons:

$(".button1").click( myNotifier.saySomething );
$(".button2").click( function () { myNotifier.saySomething()});

The first one shows: "I say: undefined"
The second one shows: "I say: HELLO"

I understand that the context (this) is not the original object when calling the method, but why is it correct when calling inside a function for the second button?

A jsfiddle to test

  • 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-11T18:52:51+00:00Added an answer on June 11, 2026 at 6:52 pm

    Have a look at MDN’s reference for the this keyword: Yes, the context depends on the way you call the method.

    If you call the function as a property of an object (like in the handler for button2), that object will be used as the context.

    However, if you use it as an event handler (it’s the same if wrapped by jQuery), the context of calling the function is the current DOM element, to which the listener is bound. And the button has no property “message”, so it will alert undefined.

    Of course, those are not the only alternatives; you might try

    var fn = myNotifier.saySomething;
    fn(); // context is the global object (window)
    

    or

    myNotifier.saySomething.call({message:"buh!"});
    

    🙂 – see MDN for explanation and more.

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

Sidebar

Related Questions

I have a question that might seem basic but I just cannot figure out
this is probably the most basic question in the world, but I cannot figure
I have installed MacVim but I cannot figure out how to do some formatting
I know this may be a basic question but I cannot find a basic
I have looked around but cannot find anything that answered this particular question related
Ok this question stems from this question: wp7: App failing! Can not figure out
This has to be a pretty basic question but I cannot for the life
This is a very basic question but I still cannot find the appropriate answer.
I apologize if the answer to this seems obvious but I just cannot figure
This is a basic question, but I cannot find the answer anywhere. I want

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.