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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:04:06+00:00 2026-06-09T21:04:06+00:00

I have created a javascript object and a function to build a new instance

  • 0

I have created a javascript object and a function to build a new instance of the object. I now need to use this object on my page but am not having any luck, I feel that I am missing something.

JS Include File

function MyObject() {
    this.ID = 0;
    this.Name = "";
}

function GetObject(param1, param2) {
    //ajax call to get json string
    $.ajax({ //leaving out details this part works
    success: function(data) {
        var jsonData = $.parseJSON(data.d);

        var myObj = new MyObject();
        myObj.ID = jsonData[0].ID;
        myObj.Name = jsonData[0].Name;

        return myObj;
    });
}

This call works fine, however when I try to access the data on the page I get undefined

Page

<script type="text/javascript">
    $(function() {
        var o = GetObject(1, 2);
    });
</script>

I was able to get it to work by passing a DOM object through to the function and assigning it there.

function GetObject(param1, param2, domObj) {
    //ajax call to get json string
    $.ajax({ //leaving out details this part works
    success: function(data) {
        var jsonData = $.parseJSON(data.d);

        var myObj = new MyObject();
        myObj.ID = jsonData[0].ID;
        myObj.Name = jsonData[0].Name;

        domObj.text(myObj);
    });
}

However this doesn’t work for my application as I’m pull a lot of objects and would like to just reference them on the page. What am I missing? Any help is greatly appreciated.

  • 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-09T21:04:07+00:00Added an answer on June 9, 2026 at 9:04 pm

    Just resign your GetObject like this:

    function GetObject(param1, param2, callback) {
        //ajax call to get json string
        $.ajax({ //leaving out details this part works
        success: function(data) {
            var jsonData = $.parseJSON(data.d);
    
            var myObj = new MyObject();
            myObj.ID = jsonData[0].ID;
            myObj.Name = jsonData[0].Name;
    
            // pass myObj via callback function parameter
            return callback( myObj );
        });
    }
    

    Now call it like:

    $(function() {
         var  o = {};
         GetObject(1, 2, function(response) {
             o = response;
         });
    });
    

    As, within GetObject there is an AJAX request (asynchronous process) and object returned after ajax success function execute, so you need to take the help of callback function.

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

Sidebar

Related Questions

I have created new rows using Javascript function addRow(id) , but I can't delete
I have a Javascript object created as follows: var ccStatTracker = (function (){ ccmap:{
I have an object I created in JavaScript. Let's say it looks like this:
I have a collection of objects in JavaScript like this: Object collection = new
I have Javascript array which contains User object. I have created this array from
Function constructors are able to create objects in javascript but I have a more
I have created a simple expand/hide test script: <html> <head> <script type=type/javascript><!-- function showHide(elementid){
So I have here a JavaScript stack built after this function: function Stack() //Creating
I have created a JavaScript extension for an array object as follows: Array.prototype.where =
I've built this piece of code...(javascript) Now we have a red sphere on the

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.