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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:06:03+00:00 2026-06-07T00:06:03+00:00

Relatively new to JavaScript, so wondering what is the best approach to achive the

  • 0

Relatively new to JavaScript, so wondering what is the best approach to achive the below requirement.
I wanted to create JSON object (RPC request object), and need to enforce some structure. As I learn, JS is dynamic language hence I can add any type to an object at any time. But, I like to define a class, which I can populate to construct the JSON request.

var RecReq = {
            ID:{},
            HeaderLevel:{},
            Content:{} //Content object goes here
        },
        HeaderLevel = {
            HeaderID:{},
            Note:{}
        },
        Content = {
            Item: [] //Array of Item goes here
        },
        Item = {
            IDs:[]
        };

    function ReceiveObj() {
        this.RecReq = RecReq,
            this.HeaderLevel = HeaderLevel,
            this.Content = Content,
            this.Item = Item
    };
    return new ReceiveObj();

I am sure many things wrong with the above code. I don’t think the object is created with the arrays initialized.
On the returned object, I cannot do push() operation to insert an iten onto Content.

How would you approach this. Do you create an object on the fly, or better to define an object which enforces some structure.

  • 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-07T00:06:05+00:00Added an answer on June 7, 2026 at 12:06 am

    If, as your code suggests, you want your instances to inherit the outer object, one approach would be to assign the prototype of the ‘class’ (be careful of thinking in class terms in JS – it’s not a class in the Java etc sense) to the object.

    var obj = { 
        prop_1: 'foo',
        prop_1: 'bar'
    }
    function Something() {
        alert(this.prop_1); //alerts "foo"
    }
    Something.prototype = obj;
    var something = new Something();
    

    This is just one way. There are many patterns for controlling inheritance (and other patterns that would achieve what you want without even going near the concept of inheritance).

    As for push() not working, in your code, Content is an object, whereas push() is a method of the Array prototype. In other words, you can push() into an array only. Perhaps you meant Content.Item, which IS an array.

    Lastly, I would avoid capitalising names. In JavaScript, this tends to be done only with functions that are used in class simulation (like your ReceiveObj), to denote that they should be instantiated, not merely invoked.

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

Sidebar

Related Questions

I'm relatively new to using oop in Javascript, and I'm wondering what the best
I'm relatively new to PHP. I'm wondering if it's possible to simply create a
Hi I am relatively new to javascript and jQuery and while trying to create
I'm still relatively new to javascript and jQuery and was just wondering this. Suppose
I'm relatively new to Javascript. I was wondering if it supports components and objects
I'm relatively new to Javascript and was wondering if there's a quick way to
So, I'm relatively new to JavaScript and I was wondering how I would add
Disclaimer: I'm relatively new to jQuery and JavaScript. The openStatement() function below executes whenever
I'm relatively new javascript, but I am comfortable in other languages. I'm trying to
Being relatively new to programming--think Programming in Objective-C by Kochan, Chapter 15--I'm wondering if

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.