2

I am new one in object oriented javascript and trying to define a class, that have an array as a data member. this data member of class storing objects of an other class as an array.

it will be more clear by this example

function classA(id, objB_01)
{

    this.id = id;   // data member that store a simple value
    this.arrayname = objB_01  // How multiple instance of classB is stored in this array 


}

function classB( id, name, status)
{
     this.id = id;
     this.name = name;
     this.status = status 
}

objB_01 = new classB("01", "john", "single");
objB_02 = new classB("02", "smith" "single");
objB_03 = new classB("03", "nina", "married");

now my question is how i can write classA so that single instance of classA hold an array that store mutiple object of classB

Something like this

objA = new classA("01",objB_01);
objA.arrayname = objB_02;
objA.arrayname = objB_03;

Now Finally objA contain one string and one array that store multiple objects of classB

Please point me in the right direction

3 Answers 3

7

One option can be to initialize an empty array in the constructor function and also have some method to add objects to the array.

function classA (id) {
    this.id = id;
    this.array = [];

    this.add = function (newObject) {
        this.array.push(newObject);
    };
}

Then you can do this:

objA = new classA("01");
objA.add(objB_01);
objA.add(objB_02);
objA.add(objB_03);
Sign up to request clarification or add additional context in comments.

Comments

4

It might do some good to just dig into the spec docs around JavaScript Arrays.

Beyond that, to answer your question about having a classA wherein a single instance has an array of multiple classB instances - I believe you're looking for the array method .push()

As well, you'll probably want to beef up your class definition, utilizing a standard overloaded constructor pattern for JavaScript.

Something like this

function classA(id, objarray)
{

    this.id = id;   // data member that store a simple value
    this.arrayname = objarray || []; //if objarray isn't passed it'll initiate an empty array 
}
function classB( id, name, status)
{
     this.id = id;
     this.name = name;
     this.status = status 
}

objB_01 = new classB("01", "john", "single");
objB_02 = new classB("02", "smith" "single");
objB_03 = new classB("03", "nina", "married");

var objBarray = [objB_01, objB_02, objB_03];

//now you can use construct & initiate classA in two different ways
//Push each object individually
var objA = new classA("01");
objA.arrayname.push(objB_01);
objA.arrayname.push(objB_02);
objA.arrayname.push(objB_03);

//or push the entire list at once
var objA = new classA("01", objBarray);

1 Comment

you code is more realistic then any other in order to initialize a classA obj when we have not any classB obj. so we can add classB objects to this after that
0

Try

        var data = {};

        var classData = function (id, name, status, options) {
            if (options) {
                this.id = options.id;
                this[options.arrayname] = [];
            };
            if (!this.hasOwnProperty("id")) {
                throw new Error("No id set on `classData` array."
                + "Please set `id` for `classData` at `options` object")
            };
            var j = (id && name && status) ? JSON.stringify({
                id: id,
                name: name,
                status: status
            }) : false;
            j ? this[Object.keys(this).filter(function (v) {
                return v !== "id"
            })[0]].push(JSON.parse(j)) : null;
            return this
        }.bind(data);
        // set `id` at `data` , 
        // add first item to `data.arrayname` 
        classData("01", "abc", "single", {"id":"01", "arrayname":"files"});
        // set data at `data`
        classData("02", "def", "married");
        classData("02", "ghi", "single");
        // read `classData`
        console.log(classData());

        var data = {};
        
        var classData = function (id, name, status, options) {
            if (options) {
                this.id = options.id;
                this[options.arrayname] = [];
            };
            if (!this.hasOwnProperty("id")) {
                throw new Error("No id set on `classData` array."
                + "Please set `id` for `classData` at `options` object")
            };
            var j = (id && name && status) ? JSON.stringify({
                id: id,
                name: name,
                status: status
            }) : false;
            j ? this[Object.keys(this).filter(function (v) {
                return v !== "id"
            })[0]].push(JSON.parse(j)) : null;
            return this
        }.bind(data);
        // set `id` at `data` , 
        // add first item to `data.arrayname` 
        classData("01", "abc", "single", {"id":"01", "arrayname":"files"});
        // set data at `data`
        classData("02", "def", "married");
        classData("02", "ghi", "single");
        // read `classData`
        console.log(classData());

1 Comment

now i understand how can we check whether or not core data member will be check by using hasOwnProperty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.