-3

I am creating a Javascript class like so:

function Board(){
 this.initializePositionArray=function(){
 var tempPositionArray=[];
 tempPositionArray[0][0]="x";
 return tempPositionArray;
 };
 this.positionArray=initializePositionArray();
}

My aim is to initially fill positionArray with values using initializePositionArray(). However, the call to initializePositionArray() gives the following error:

Uncaught ReferenceError: initializePositionArray is not defined
asked May 9, 2017 at 7:43
3

1 Answer 1

2

You have to call it with this:

this.positionArray=this.initializePositionArray();

Because it is a property of your constructor and you were calling it as a global function which is not defined.

answered May 9, 2017 at 7:44
Sign up to request clarification or add additional context in comments.

5 Comments

Should you not look for dupes and close it?
Oh is it? not looked it though. and it doesn't seem to be dupe as per your suggestion.
@Rajesh, is it actually a duplicate? I am defining my object in a different way than in the proposed duplicate question.
@sigil Its not about how you define the object. At the end you want to call a method inside same object. Yes accepted answer is not the desired answer but other answer addressed it correctly

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.