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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:20:38+00:00 2026-05-30T02:20:38+00:00

I was making a test case to show how ‘bind’ is necessary for a

  • 0

I was making a test case to show how ‘bind’ is necessary for a method to refer to its function in a callback.

But just when I thought I knew JS – the following code works fine – without requiring bind!

pretendThingConstructor = function (greeting) {
    this.greeting = greeting;
    this.sayHello = function() {
        console.log(this.greeting);
    };
}

var pretend_thing = new pretendThingConstructor('hello world');

pretend_thing.sayHello();

setTimeout(function() {  
    pretend_thing.sayHello()
}, 3000);

When I run it – via node, phantomjs, or another JS environment – it works. ‘hello world’ is printed twice.

I expected the second ‘hello world’ – the one ran after the timout – to fail, as ‘this’ would refer to the event, rather than the object. But it works. Why is this?

  • 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-30T02:20:39+00:00Added an answer on May 30, 2026 at 2:20 am

    The this changes depending on how your call the function. If you specify a base object, it will refer to that instead:

    pretend_thing.sayHello()
    

    Here pretend_thing is that base object and therefore this still refers to that object. On the other hand, if you had:

    var f = pretend_thing.sayHello;
    f();
    

    Here this should refer to window object instead.

    You can confirm it by putting:

    console.log (this instanceof pretendThingConstructor);
    

    Inside your sayHello function. It will print true in both cases.


    pretendThingConstructor = function (greeting) {
        this.greeting = greeting;
        this.sayHello = function() {
            console.log(this.greeting);
            console.log(this instanceof pretendThingConstructor);
        };
    }
    
    var pretend_thing = new pretendThingConstructor('hello world');
    ////////////////////////////
    
    pretend_thing.sayHello();
    
    setTimeout(function() {  
        pretend_thing.sayHello();
    }, 3000);
    

    will output:

    true
    true
    

    whereas:

    var f = pretend_thing.sayHello;
    f();
    

    outputs:

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

Sidebar

Related Questions

We are automating test cases, and one particular test case involves making the application
Im making an easy application just to test the JSF framework. This is actually
OK, so the @Ignore annotation is good for marking that a test case shouldn't
I am making a test fullscreen application using this guide . However, I am
I'm starting with Ruby, and while making some test samples, I've stumbled against an
I'd like to test my knowledge in EJB, making an small application. Could you
I have a unit test called TestMakeAValidCall() . It tests my phone app making
I have code like: document.onmousedown = function(){ alert('test'); } Now, except the element with
I'm making a small test framework that uses the JavaScript module pattern for UI
I know that Test Case Management app silently records intellitrace data dump files (

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.