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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:39:13+00:00 2026-05-24T23:39:13+00:00

I have a following (simplified) code: var myModule = { submitDummyForm: function(){ console.log(this); //

  • 0

I have a following (simplified) code:

var myModule = {

    submitDummyForm: function(){ 
        console.log(this); // the right object is logged out
        var that = this; // keep a reference
        $.ajax({
            type:'POST',
            url: 'http://localhost/',
            data: {dummyData: 'something'},
            dataType: 'json',
            success: that.dummyFormSuccess

        });
    },

    dummyFormSuccess: function(data){ 
        console.log(this); // 'this' is logged out as some foreign object, most probably jQuery.ajax object
    }
}

It leads to ‘this’ being lost in the dummyFormSuccess, no matter if I use this.dummyFormSuccess or that.dummyFormSuccessfor as an argument for my ajaxSubmitForm().

But the following code gets executed as I need:

var myModule = {

    submitDummyForm: function(){ 
        console.log(this); // the right object is logged out
        var that = this; // keep a reference
        $.ajax({
            type:'POST',
            url: 'http://localhost/',
            data: {dummyData: 'something'},
            dataType: 'json',
            success: function(data) {
                 that.dummyFormSuccess(data);
            }
        });
    },

    dummyFormSuccess: function(data){ 
       console.log(this); // now 'this' is logged out correctly as the real myModule object
    }
}

I’m still not very comfortable with advanced topics of Javascript but I already know, that ‘this’ may get redefined, depending on where it is used. I thought if I use ‘that’ to store the reference to ‘this’, it should also keep my ‘this’ inside the called function. It seems weird, that I can call that.dummyFormSuccess(data) in a wrap-around function and it gets correct ‘this’ inside, but if I just assign it to $.ajax success, my ‘this’ gets lost.

Can anybody explain, where is ‘this’ getting lost in my case and why it works OK in the second example? Is it a problem with jQuery (maybe jQuery.ajax() overwrites my ‘this’ somehow in my case) or just a feature of the language?

  • 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-24T23:39:14+00:00Added an answer on May 24, 2026 at 11:39 pm

    Everything is correct. Your this is lost in a first example because you are assigning function that.dummyFormSuccess to jQuery ajax object’s success. So, this way, deep inside jQuery, it’s called something like ajax.success. So, this is overwritten with ajax object.

    With second approach you create an anonymous function and assgn it to success. So inside your anonymous function, this points to ajax object, but that variable is accessible and have not been overwritten.

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

Sidebar

Related Questions

I have the following code, which i've simplified, where $currEl gets logged and displays
I am debugging some code and have encountered the following SQL query (simplified version):
I have the following code (simplified for the sake of discussion). What I don't
I have the following code(simplified). public class OrderProcessor { public virtual string PlaceOrder(string test)
I have the following code (asp.net-mvc, jquery) (i have simplified the example to show
I have code (simplified) Configuration: _container.AddComponent<IRepository<Project>, FakeProjectRepository>(); var instance = new List<Project>(); _container.Kernel.AddComponentInstance<IList<Project>>(instance); Class:
I have the following javascript function in my extension: function findPos(obj) { var curleft
In my controller I have the following simplified code: def index @dashboard_items = []
In a Rails application using prototype, I have the following (simplified) code in my
Consider the following simplified version of my code. I have a template class A

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.