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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:37:56+00:00 2026-05-17T19:37:56+00:00

I’m attempting to make a class in javascript. I create it with the JSON

  • 0

I’m attempting to make a class in javascript. I create it with the JSON type object thing.

Doing this:

Foo = {
   PubId: '',

   Init:function( oCallback )
   {
         this.sendCommand( 'INIT', {}, oCallback );
   },

    sendCommand : function( sCommand, aParams, oCallback )
    {

        setTimeout( oCallback, 1000, '{"response":"INIT","time":1287982024,"pubid":"4cc50bc47c7b3"}' );

        return true;
    },
    onData : function( sData )
    {
        var aRes = JSON.parse( sData );

        this.PubId = aRes.pubid;
        alert( this.PubId );
        return this.PubId;
    },
    umtest:function(){ alert( this.PubId ); }
}

I then also do this after including the script:

Foo.Init( Foo.onData ); 

The problem is that the this.PubId is updated inside the onData method, but outside of it, the pubid is empty.

I am pretty new at javascript classes, so I’m not sure what needs to be done so I was hoping someone could help meh out. 🙂

Thanks for your time!

  • 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-17T19:37:56+00:00Added an answer on May 17, 2026 at 7:37 pm

    Well you’ve got two problems here. The first problem is not understanding how this works in Javascript. When Foo.onData is called via setTimeout( oCallback, ...) the this will reference to the global object not Foo.

    In order to call it with Foo as this you should change your code:

    sendCommand: function (sCommand, aParams, oCallback) {
        var that = this;           // save this reference
        setTimeout(function () {
            oCallback.call( that,  // call the function with it
                   '{"response":"INIT","time":1287982024,"pubid":"4cc50bc47c7b3"}' );
        }, 1000);
        return true;
    },
    

    In order to test what’s changed place this code to onData:

    // is `this` really Foo or the global object?
    alert(this === Foo);    // should be true
    alert(this === window); // should be false
    

    In the updated version this will correctly reference Foo as it’s object of invocation.

    The second problem you might be facing with is that your function called with setTimeout will only be executed after 1000 ms = 1s, so if you’re simply checking alert(Foo.PubId) outside of Foo you will get an empty string (because the callback hasn’t been called yet).

    In order to test if Foo.PubId is indeed changed:

    // execute the check after 2s so as to
    // make sure the callback has been called
    setTimeout( function () {
      alert(Foo.PubId);
    }, 2000);
    

    You can check the full test case here.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.