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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:41:58+00:00 2026-06-06T23:41:58+00:00

I have a problem with my class where I set a callback function to

  • 0

I have a problem with my class where I set a callback function to fire after an ajax request, this works fine except for the callback then having this= window instead of this = the calling class. I have tried fiddling with the bindings etc to no avail. any help appreciated.

<?php
if (isset($_POST['id'])){
    echo json_encode(array('key' => 'val'));
    die;
}
?>
<script type="text/javascript" src="/linkup/js/mootools-core-1.4.3-full-compat-yc.js" charset="utf-8"></script>
    <script type="text/javascript" src="/linkup/js/mootools-more-1.4.0.1.js" charset="utf-8"></script><script>
var myClass = new Class({
    blah: function(){
        console.log('this worked ok');
    },
    foo: function(){
        this.blah(); // at this point it is failing with "unknown function"
    },
    bar: function(){
        this.reqJSON({id:1,req:'grabpoints'},this.foo);
    },
    // data - an aray containing some data to post {id:1,req:'grabpoints'}
    // onS - a function to fire on return of data
    reqJSON: function(data,onS){
        if (this.jsonRequest) this.jsonRequest.cancel();
        this.jsonRequest = new Request.JSON({
                url: 'test.php',
                onRequest: function (object){
                    console.log('sending json');
                },
                onSuccess: function (object){
                    console.log('json success');
                    onS(object);
                }
            }
        ).post(data);
    },

});
var o = new myClass();
o.bar();

  • 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-06T23:41:59+00:00Added an answer on June 6, 2026 at 11:41 pm

    this is never part of an inherited scope (i.e. the closure) for a function, but instead determined when the function is called, in this case by onS(object). Called this way, the function is just that – a function. You need to call this.onS(object), but this won’t work in your case because the onSuccess function does not know about this either.

    To call the foo/onS function with the outermost object as this you must save a reference to it in another variable, commonly called that:

    reqJSON: function(data,onS){
        var that = this;
        ...
    
        onSuccess: function (object){
            console.log('json success');
            that.onS(object);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that specifies a set of callback functions (shown here as
Good day, I have the following problem: class B extends class A and methods
I have a design problem: Question class: have a question and answer (String) TextQuestion
I have a problem with ViewHolder class. I use ViewHolder to improve my List
I have problem organizing my unittest based class test for family of tests. For
I have a problem to test my non activity-class which need the context of
I have a problem in calling a template class I have. I declared a
i have a problem calling multiple instance of a class that i have coded
I have the problem that my view controller class has too many delegates and
I have a problem with getting a class from an external .jar file. 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.