0

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.

3
  • what is 'activeWSId'? Commented Dec 12, 2013 at 9:58
  • 1
    check the fiddle jsfiddle.net/6Pz7R Commented Dec 12, 2013 at 10:05
  • Thanks Anand, I got the issue, it was the missing this keyword Commented Dec 12, 2013 at 10:18

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked. Actually I read this one tutorial but missed this keyword :(.

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.