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
sigil
9,63244 gold badges129 silver badges217 bronze badges
1 Answer 1
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
Jai
74.8k12 gold badges77 silver badges104 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Rajesh Dixit
Should you not look for dupes and close it?
Jai
Oh is it? not looked it though. and it doesn't seem to be dupe as per your suggestion.
sigil
@Rajesh, is it actually a duplicate? I am defining my object in a different way than in the proposed duplicate question.
Rajesh Dixit
@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 Dixit
@sigil this might help: stackoverflow.com/questions/10918228/…
lang-js
thisin context