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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:25:33+00:00 2026-06-10T22:25:33+00:00

Note : the code looks long but its simple psuedo code for demo purpose.

  • 0

Note: the code looks long but its simple psuedo code for demo purpose. Don’t flee!

I have written a widget that listens for key presses. When the onKeyDown event is fired the reference to this is lost. So I set a scoped variable called self to reference the widget. self is initalised when the widget is created.

This works but I’m not sure this is the right approach. I think the solution here is to do with a proxy? perhaps the self variable is already acting as a proxy? anyone want to shed some light on whats happening?

(function($)
{   
    var vendorPrefix = Modernizr.prefixed("transform");
    var self;

    $.widget("ui.myWidget", {

        _create: function()
        {  
            self = this;            
            //Do some widget stuff  
            document.addEventListener('keydown', this.onKeyDown, false)
            return this;
        },

        exampleFn: function()
        {
        },

        onKeyDown: function(event)
        {
            // this now refers to "document" and not the widget             
            switch(event.keyCode)
            {
                case 37: 
                    self.exampleFn();
                    break;
                case 39:
                    self.exampleFn();
                    break;
            }
        }

    });
})(jQuery);
  • 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-10T22:25:35+00:00Added an answer on June 10, 2026 at 10:25 pm

    You can also use jQuery.proxy() if you’d like to get rid of the self variable:

    onKeyDown: $.proxy(function(event)
    {
        // this now refers to "document" and not the widget             
        switch(event.keyCode)
        {
            case 37: 
                this.exampleFn();
                break;
            case 39:
                this.exampleFn();
                break;
        }
    }, this)
    

    Anyway, what’s happening is that this in a function refers to that function. So this in onKeyDown is onKeyDown function. If you use a proxy like I did above, this in onKeyDown will become a reference to the parent function.

    More on this behavior in JavaScript: How does the "this" keyword work?

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

Sidebar

Related Questions

I have the following HTML code (note no closing tags on <\option> ) generated
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
let's say I have a html structure like this -> (note: in my code
Note: I'm sorry if this is an extremely simple question but I'm somewhat obsessive
Its been a long time since I had to do anything but minor fixes
I have some code that looks something like this: <head runat="server"> <script type="text/javascript"> var
Note my code below. I am trying to figure out why my data is
Note: This code actually codes from a tutorial book I'm reading at the moment,
Here is the code: //Note: x actually isn't defined, I'm pulling it from an
System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself

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.