-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
sigil
  • 9,370
  • 40
  • 119
  • 199

1 Answers1

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.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • Should you not look for dupes and close it? – Rajesh May 09 '17 at 07:45
  • Oh is it? not looked it though. and it doesn't seem to be dupe as per your suggestion. – Jai May 09 '17 at 07:46
  • 1
    @Rajesh, is it actually a duplicate? I am defining my object in a different way than in the proposed duplicate question. – sigil May 09 '17 at 07:48
  • @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 – Rajesh May 09 '17 at 07:49
  • @sigil this might help: http://stackoverflow.com/questions/10918228/javascript-calling-object-methods-within-that-object – Rajesh May 09 '17 at 07:51