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 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

Related Questions

I have realy strange behavior. I have this code (I remove unrelated code). $(function()
Consider this javascript code: var bar = function () { alert(A); } var foo
The below code is taken from http://bonsaiden.github.com/JavaScript-Garden/ function Foo() { this.value = 42; }
Does this function has the same behavior that memset ? inline void SetZeroArray( void
I get this behavior in both Chrome (Developer Tools) and Firefox (Firebug). Note the
I've had this behavior happen to me twice recently and I was wondering what
I don't understand this behavior in javascript for inheritance I've always seen it defined
What I want is this behavior: class a: list = [] x = a()
I would like to have this behavior in a task history stack: First task
I'd like to understand the reason for this behavior of OCAML objects. Suppose I

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.