0

I want to use the class Coordinates's methods compX and compY outside the class and inside another function how can I do this I am getting the error ReferenceError: xy is not defined

I tried making the object inside the other function drawGraph() but it still gives an error.

<script>
var c =document.getElementById("myCanvas"); 
var ctx = c.getContext("2d");
//function call
drawGraph(ctx,-10,10);
drawAxis(ctx,10);

function f(x){
        return x*x;
}

class Coordinates {
    Coordinates(){}
    compX(x){
        originX = myCanvas.width/2;
        mag = 20;
        return(originX + mag*x);
    }
    compY(y){
        originY = myCanvas.height/2;
        mag = 20;
        return(originY - mag*y);
    }
}

xy = new Coordinates();
function drawGraph(ctx, a, b){
//make lines that trace coordinates of the function in an interval
    var n = 50;
    var dx  = (b-a)/n;
    for(x=a; x<=b; x+=dx){
    var pointAx=x;
    var pointAy=f(x);
    var pointBx=x+dx;
    var pointBy=f(x+dx);
    console.log(`[${pointAx}, ${pointAy}],[${pointBx}, ${pointBy}]`)


    ctx.moveTo(xy.compX(pointAx), xy.compY(pointAy)); //this is where the error occurs
    ctx.lineTo(xy.compX(pointBx), xy.compY(pointBy));
    ctx.stroke();
    ctx.strokeStyle = "#0000ff"
    ctx.lineWidth = 1;
    } 
}

function drawAxis(ctx, axisLength){
    ctx.strokeStyle = "#000000";
    ctx.moveTo(xy.compX(0), xy.compY(axisLength));
    ctx.lineTo(xy.compX(0), xy.compY(-axisLength));
    ctx.moveTo(xy.compX(axisLength), xy.compY(0));
    ctx.lineTo(xy.compX(-axisLength), xy.compY(0));
    ctx.stroke();
    ctx.lineWidth = 1;
}

</script>

I am getting the error ReferenceError: xy is not defined I was expecting a graph to be plotted but nothing got displayed please also tell how i can make the code better(neat)

Manik
  • 573
  • 1
  • 9
  • 28

1 Answers1

2

Several things are wrong here, here is the better version of the code:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

class Coordinates {
    compX(x) {
        var originX = myCanvas.width / 2;
        var mag = 20;
        return originX + mag * x;
    }
    compY(y) {
        var originY = myCanvas.height / 2;
        var mag = 20;
        return originY - mag * y;
    }
}

var xy = new Coordinates();

drawGraph(ctx, -10, 10);
drawAxis(ctx, 10);

function f(x) {
    return x * x;
}

function drawGraph(ctx, a, b) {
    //make lines that trace coordinates of the function in an interval
    var n = 50;
    var dx = (b - a) / n;
    for (x = a; x <= b; x += dx) {
        var pointAx = x;
        var pointAy = f(x);
        var pointBx = x + dx;
        var pointBy = f(x + dx);
        console.log(`[${pointAx}, ${pointAy}],[${pointBx}, ${pointBy}]`);

        ctx.moveTo(xy.compX(pointAx), xy.compY(pointAy)); //this is where the error occurs
        ctx.lineTo(xy.compX(pointBx), xy.compY(pointBy));
        ctx.stroke();
        ctx.strokeStyle = "#0000ff";
        ctx.lineWidth = 1;
    }
}

function drawAxis(ctx, axisLength) {
    ctx.strokeStyle = "#000000";
    ctx.moveTo(xy.compX(0), xy.compY(axisLength));
    ctx.lineTo(xy.compX(0), xy.compY(-axisLength));
    ctx.moveTo(xy.compX(axisLength), xy.compY(0));
    ctx.lineTo(xy.compX(-axisLength), xy.compY(0));
    ctx.stroke();
    ctx.lineWidth = 1;
}

First, get used to writing var/let/const before variable assignment. That was causing your some of the erros you've had. Second, as several people mentioned in the comments, you were getting the error because the function drawGraph() called xy before xy got assigned. Moving xy higher in the definition should solve your problem of xy unassigned.

Then, do not use empty constructor like you did. If you have something in the constructor, define it as constructor() and not className().

Dragos Strugar
  • 1,314
  • 3
  • 12
  • 30