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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:19:57+00:00 2026-06-12T11:19:57+00:00

I thought I’d got my head around context and scope in javascript. I don’t

  • 0

I thought I’d got my head around context and scope in javascript. I don’t understand why in one situation proxy/bind works, and in another it doesn’t. Please could someone explain?

Example 1: Proxy (or bind) works:

function Cat(name){
    this.name = name;

    $("#cat").click(
        $.proxy(
            function(e){ this.meow(e); }
        , this)
    );

    this.meow = function(){ alert(this.name + "says meow"); }
}
var cat = new Cat();

Example 2: Proxy and bind don’t work:

function Dog(breed){
    this.breed = breed;

    this.save = function(){
        var that = this;
        $.ajax({
            url: '/ajax/savedog.php', dataType: 'json',

            // This works?? Shouldn't scope be of .ajax()?
            data: this.breed, 

            // success: $.proxy(... // won't work? why?
            success: that.dogSaved, error: ajaxFail
        });
    };

    this.dogSaved = function(){ alert("Dog Saved"); }
}
var dog = new Dog();
  • 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-12T11:19:58+00:00Added an answer on June 12, 2026 at 11:19 am

    You’re copying this to that inside the “click” handler. Do it outside.

    The value of this is set anew for and upon each function call. Thus, inside your “click” handler, this is not a dog, it’s a DOM element.

    It’s also important to be mindful of the order of evaluation in JavaScript. Parameters to functions are fully evaluated in the context of the calling environment. In your first example, the value of this passed in to $.proxy() is the correct one because it’s a parameter in the context of the Cat constructor, and thus it is correctly a cat. In your call to $.ajax in the Dog constructor, however, the reference to this.breed will not have the correct value, as it is evaluated in the context of the “click” handler and not the constructor. The value of the “data” property should be that.breed once you’ve fixed the initialization of that as described above.

    (Also note that as @Musa points out, the Dog constructor is syntactically incorrect; I assume that was just a transcription error.)

    edit — I’m not sure what you’re trying with $.proxy() in your updated question, but that “success” property should work if you set it up like this:

       success: $.proxy(that.dogSaved, that),
    

    The first argument to $.proxy() is the function to be called, and the second is the value you want for this when the function is called. In this case, you want it to be the saved reference to the dog object. Note, however, that it will only work if the “save” function is called with this set to a dog. That is, if somewhere you do this:

       var fido = new Dog();
       fido.save();
    

    then things should work. However, if you somehow use that “save” function in some way such that this is not a reference to a dog, then it won’t work. Again, if in the “Dog” constructor you were to move that declaration of “that” out to the constructor scope instead of in “save”, then it won’t matter how “save” is called because “that” will always be a reference to the dog object.

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

Sidebar

Related Questions

I thought the following script will create div element but I got nothing output
Thought of making a small textEditor. It will contain two textareas, one for entering
I thought one feature of dynamic libraries (and by extension Apple's Mach-O Frameworks) was
I thought that text will flow around a floated element if the text follows
Thought I would throw this one out there and see what other people's experiences
Thought I will point out first that I have looked around on Stackoverflow and
I thought text-align works in Twitter Bootstrap but it doesn't? I'm not sure what's
I thought I had this one pegged by mapping the intermediary table as a
I thought MVC3 can bind JSON data to model by default. but this code
I thought the point of a singleton was I could only initialize one instance

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.