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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:27:02+00:00 2026-05-11T11:27:02+00:00

The behavior of this when function bar is called is baffling me. See the

  • 0

The behavior of ‘this’ when function bar is called is baffling me. See the code below. Is there any way to arrange for ‘this’ to be a plain old js object instance when bar is called from a click handler, instead of being the html element?

// a class with a method  function foo() {      this.bar();  // when called here, 'this' is the foo instance      var barf = this.bar;     barf();   // when called here, 'this' is the global object      // when called from a click, 'this' is the html element     $('#thing').after($('<div>click me</div>').click(barf)); }  foo.prototype.bar = function() {     alert(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. 2026-05-11T11:27:02+00:00Added an answer on May 11, 2026 at 11:27 am

    Welcome to the world of javascript! 😀

    You have wandered into the realm of javascript scope and closure.

    For the short answer:

    this.bar() 

    is executed under the scope of foo, (as this refers to foo)

    var barf = this.bar; barf(); 

    is executed under the global scope.

    this.bar basically means:

    execute the function pointed by this.bar, under the scope of this (foo). When you copied this.bar to barf, and run barf. Javascript understood as, run the function pointed by barf, and since there is no this, it just runs in global scope.

    To correct this, you can change

    barf(); 

    to something like this:

    barf.apply(this); 

    This tells Javascript to bind the scope of this to barf before executing it.

    For jquery events, you will need to use an anonymous function, or extend the bind function in prototype to support scoping.

    For more info:

    • Good explanation on scoping
    • Extending jQuery bind to supportscoping
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 259k
  • Answers 259k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Something like this should work: var existingUsers = from u… May 13, 2026 at 11:07 am
  • Editorial Team
    Editorial Team added an answer Just doing a :bd should do it. Edit: You can… May 13, 2026 at 11:06 am
  • Editorial Team
    Editorial Team added an answer Just to close this question: I restarted the device, and… May 13, 2026 at 11:06 am

Related Questions

Up until today, I had always thought that decent compilers automatically convert struct pass-by-value
I wrote some JavaScript to allow editing a list of items within an HTML
I use Launch4j as a wrapper for my Java application under Windows 7, which,
A coworker has been using a custom AVI to indicate progress during some longer

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.