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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:48:08+00:00 2026-06-15T11:48:08+00:00

I have a problem using Class with setter using ajax. Here is the condition.

  • 0

I have a problem using Class with setter using ajax. Here is the condition.

var MyClass = Class.create();
MyClass.prototype = {
    myAttr:0,
    setMyAttr: function(){
        jQuery.ajax({
            url:'data.php',
            success:function(data) {
                //set myAttr here
            }
        });
    }
}

I want to set myAttr inside the success function, how to do this? Thanks. 😀

  • 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-15T11:48:10+00:00Added an answer on June 15, 2026 at 11:48 am

    You haven’t said what Class.create is (there is none in JavaScript itself), but I don’t think it matters much for the question.

    I assume you want to set myAttr on the instance, not the prototype (but see below if I’m wrong). The simplest way is to take advantage of the fact that your success callback is already a closure over the context of the call to setMyAttr, so you set a variable to be this (since this will be different in the success callback) and use that:

    var MyClass = Class.create();
    MyClass.prototype = {
        myAttr:0,
        setMyAttr: function(){
            var self = this;            // <== Set the variable
            jQuery.ajax({
                url:'data.php',
                success:function(data) {
                    self.myAttr = data; // <== Use it
                }
            });
        }
    };
    

    More on closures: Closures are not complicated

    But if I’m wrong and you do actually want to update the prototype instead:

    var MyClass = Class.create();
    MyClass.prototype = {
        myAttr:0,
        setMyAttr: function(){
            jQuery.ajax({
                url:'data.php',
                success:function(data) {
                    MyClass.prototype.myAttr = data;
                }
            });
        }
    };
    

    (Note the ; at the end of the assignment to the prototype. I strongly advocate not relying on the horror that is automatic semicolon insertion.)

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

Sidebar

Related Questions

I have a problem with a query using hibernate. The entity class EntityClass has
Hi all im using a sqlite helper class, but i have a little problem
I have a class that is mapped to a table using NHibernate. The problem
I have a problem using JSON and arrays. Here is my code: while($row =
I have implemented a generic IPropertyChangedNotifier using castle dynamic proxy. Here I intercept setter
I have problem using arabic font for iOS. All fonts have the same render,
i have problem using LIKE structure in DB2 : for example: select * from
I have problem while using jquery maskedinput with asp.net textbox. I have a check
I have a problem using a local SQL Server CE database with C# and
I have a problem using Linq to NHibernate to load an object and eagerly

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.