I’m building a p5 library and I’m new to JavaScript. I’m confused by the JavaScript closure and array system. I don’t understand why I get undefined object “field” in for loops.

Ps: I’m using the math.js eval to translate String to function.

Here is my code

```
p5.prototype.vectorField = function (fx,fy,size){
let field = new Array();
// this is correct.
console.log(Array.isArray(field));
if( typeof fx == "string" ){
for(let i = 0; i < size; i++){
let scopex = {x:i};
let x = math.eval(fx,scopex);
for(let j = 0; j<size; j++){
let scopey = {y:j};
let index = i+(j*size);
let y = math.eval(fy,scopey);
// but I get error here!
if (!(index in field)){
// and here!
field.push([]);
}
field[index].push(p5.Vector(x,y,0));
}
}
}
console.log(field);
return field;
};
```

And it throws error like this : “Object doesn’t support property or method ‘push’”