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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:46:36+00:00 2026-05-26T13:46:36+00:00

function test() { this.str = hello; this.sayHello = function() { document.write(this.str); } this.init =

  • 0
function test() {
    this.str = "hello";

    this.sayHello = function() {
        document.write(this.str);
    }

    this.init = function() {
        document.onkeydown = this.sayHello;
    }
}

var testing = new test();
testing.init();

The above code should output “hello” on an onkeydown event.
But I get “undefined”. How can I get this to work ?

  • 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-26T13:46:37+00:00Added an answer on May 26, 2026 at 1:46 pm

    The problem is with this.sayHello. When you assign the reference to the sayHello function on keydown, the reference to the context (object) is lost. When a key is pressed, this refers to the Document object as the callback is invoked as:

    document.onkeydown(); // or for simplicity imagine - document.sayHello();
    

    If you assigned the str variable on the document object, you would see the value logged,

    document.str = "hello";
    

    However, that is not what you’d want. You need to wrap the keydown event handler inside another function to preserve the context to that object. Two ways to go about this. You could either wrap the event handler inside another function, and preserve the reference to this.

    this.init = function() {
        var me = this;
        document.onkeydown = function() {
            me.sayHello();
        };
    }
    

    Or, if you’re using modern browsers, this has already been incorporated into ECMAScript 5 using the bind function.

    this.init = function() {
        var me = this;
        document.onkeydown = this.sayHello.bind(this);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code.. $(document).ready(function(){ $(#test a).click(function(){ var labelTo = $(this).text(); window.location =
this code always return 0: function myFunc() { var str = 0; JQuery.ajax({ type:
I have just come across this code: function test(){ //... if ( $profilerule ==
why does this only alert 1 ? function test() { var myobj = {
I used a code: jQuery.fn.MyFunction = function(){ return this.each(function() { attributes = test; return
var x= 1; Number.prototype.test = function () { return this }; x.test() === x.test()
This example is from php.net: <?php function Test() { static $a = 0; echo
I have this (simplification): $(li).click(function{alert(test);}); <li> <input> </li> What's the best way to bind
Could someone tell me if this: $.ajax({ url: 'test.html', success: function(data) { alert(Data Loaded:
So given this input string: =?ISO-8859-1?Q?TEST=2C_This_Is_A_Test_of_Some_Encoding=AE?= And this function: private string DecodeSubject(string input) {

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.