codeomnitrix
codeomnitrix

Reputation: 4249

unable to create array of objects

I am bit new to object oriented programming using javascript.

I have an array of workspace as

//global var
var workspaceArray = new Array();

then I am pushing a workspace object in the array as -

//in some function
workspaceArray.push(new wsObj());

//wsObj function
function wsObj(){
    states = new Array();
    links = new Array();
}

But when I try to use it somewhere it throws error that cannot read property state.

//error in the following line
var stateName = "q" + "<sub>" + workspaceArray[activeWSId].states.length + "</sub>";

Thanks in advance.

Upvotes: 0

Views: 111

Answers (1)

Willem Mulder
Willem Mulder

Reputation: 13994

You create states and links as global variables, instead of assigning them to the created Object. Assign them like like this

//wsObj function
function wsObj(){
    this.states = new Array();
    this.links = new Array();
}

And it will work!

Upvotes: 2

Related Questions