c398396 c398396 - 1 year ago 102
Javascript Question

How do you create a serialized set of objects nested within another one?

At the end of the day, I need a javascript function to help me create:

object question[0]= {questionText ,Answer[{answerText1, icon1}, answerText2, icon2},.....]}

I basically have a question class that has questions with question text, and any number of answers. Questions are in order. Answers is a class of its own, where each answer has ANY number of answers, with answer text, and an icon. I'm not sure if I've declared it correctly, but here is my attempt at the function to create this question object:

function Question(qText, A) {
this.questionText = qText;
this.Answer = A;
function Answer(aText, icon) {
this.answerText = aText;
this.icon= icon;

var AnswerForQuestion1[0]= new Answer {"Male", MaleIcon}
var AnswerForQuestion1[1]= new Answer {"Female", FemaleIcon}]
var question[0]= new Question ("What's your Gender?",AnswerForQuestion1)

But this creates unnecessary answer variables. Without knowing how big the answer for the question is going to be, how do I create one function that can create a question with answers at once?

Answer Source

You are using var to redefine the variable of the array each time? Also, you have consistently inconsistent use of invalid syntax.

Anyways, it is good you are thinking object-oriented. I think like this: the "final" result you are trying to see provides a question and some answers. It seems appropriate to make this an object, call it a QA object. This way, you are essentially able to create an instance of that business-logic-specific pair. You can pass these instances around easily & understandably.

function QA(question, answerCollection) {
    this.question = question;
    this.answerCollection = answerCollection;

QA.prototype.getQuestion = function() { }; //whatever

Thinking about implementing further (you've thought this through, skipping the first step), I think of some sort of object to represent an instance of a question, and some sort of object to represent an instance of an answer (these are called entities). Additionally, thinking about the future, I may want some sort of collection interface (it actually acts as a data collection factory) to 'create' answer collections:

 * Entities

function Question() {
    this.question = this.icon = NULL;

Question.prototype.setQuestion = function() { }; //whatever

function Answer() {
    this.answer = this.icon = NULL;

Answer.prototype.setAnswer = function() { }; //whatever

 * Factory

var AnswerCollectionFactory = {};

AnswerCollectionFactory.build = function(arr) {
    var tmpAswer, collection = []

    for(var key in arr) {
        tmpAswer = new Answer();


    return collection;

Following this/a similar implementation, readable & portable code instantiates what you want:

var question = new Question;
question.setQuestion('What\'s your gender?');
question.setIcon(); //whatever

var answerCollection = AnswerCollection.build([
    {answer: ..., icon: ...},
    {answer: ..., icon: ...}

new QA(question, answerCollection); //there you ago

At the end of the day: if you don't need to use user-defined objects & new, you can just go with the simpler object solution:

var qa = {
    question: {question: ..., icon: ...},
    answers: [
        {answer: ..., icon: ... },
        {answer: ..., icon: ... }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download