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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:39:29+00:00 2026-05-23T23:39:29+00:00

The question In Javascript, how can an event handler function refer to members of

  • 0

The question

In Javascript, how can an event handler function refer to members of its parent? i.e. can you define an event handler function part of a larger object and have that function "know about" its parent?

(Note that someone else posted a nearly identical question Accessing variables of parent function in Javascript event handlers . It hadn’t been answered. Hence the repost )

Erroneous Presuppositions

I had thought that at "function definition" time you could capture a copy of "this" for later re-use, (e.g. copy to "self"). Evidently I was wrong: after I bind the function to the event ("click()" below), "self" subsequently refers to the html anchor tag (”);

The general context :
trying to use encapsuation/object-orientedness for code-re-use in javascript.

Example

Here’s a simple example (cribbed from elsewhere and modified).

The function succeeds when called during page load, but fails when the user preses "click"

<a href="#" id="myLink" rel="my link">MY LINK</a>

<script type="text/javascript">

var Construct = function() {
    var self = this;    

    this.attr1 = 3;
    this.attr2 = 2;
    this.childObj = {
        method1: function () {
            // this function fails if called from an event handler
            // edited this function to "do something", i.e. provide a visual cue upon execute
            var foo = self.attr1 * self.attr2;
            alert ('value is ' + foo);
            return foo;
        }
    }
}

     var obj = new Construct();
    
    // this call succeeds
    alert (obj.childObj.method1());   

    //this call fails as soon as the event handler refers to "self"
    $("#myLink").click(obj.childObj.method1);

</script>
</body>
</html>

Update/Edit
-Updated the example to give a ‘visual cue’ when it runs
-added this section.

My error. As pointed out below, the example works fine. My original, non-working code used this:

   this.self = this

instead of

   var self = this

I didn’t realize the distinction (in Java they would be identical) and missed the fact that my example actually worked, (whereas my code failed).

  • 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-05-23T23:39:30+00:00Added an answer on May 23, 2026 at 11:39 pm

    Your code works fine. self refers to the object as it is supposed to. That’s how the lexical scoping of javascript is defined.

    The problem is your handler does nothing. method1 simply returns 6 but you never tell it do anything with that value. If you want to prove it to yourself, the line before the return, add an alert: alert(self.attr1 * self.attr2);

    Working Example

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

Sidebar

Related Questions

The Question: Is there a way that I can pass a JavaScript function into
Strange question, I know. Allow me to explain. I have a JavaScript/jQuery function that
HTML Javascript question to get the selected value of a input-select I can use
I have javascript string variable with var sttr=We prefer questions that can be answered
I have a question on javascript window management. My application opens an iframe which
I have a simple question concerning Javascript. I am trying to print in a
I am making a webapp. I have a fairly basic question about javascript performance.
I got a question regarding C++ Object & Javascript Object life cycle mis-synchronization and
have dumb question. Here I've got three events calling the same function, connected to
I've installed a handler for the click JavaScript event of a <button> element using

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.