3

I get the error:

Uncaught TypeError: rect.drawIt is not a function

I don't understand why this error happens? I mean i am doing the same as here: http://www.w3schools.com/js/js_object_prototypes.asp

  var context = document.getElementById("canv").getContext("2d");
    
    for (var k = 0; k < 4; k++) {
        var rect = new Recta();
        rect.drawIt();
    }
    
    function Recta() {
        this.x1 = Math.floor(Math.random() * context.canvas.width);
        this.y1 = Math.floor(Math.random() * context.canvas.height);
        this.x2 = Math.floor(Math.random() * context.canvas.width);
        this.y2 = Math.floor(Math.random() * context.canvas.height);
        this.x3 = Math.floor(Math.random() * context.canvas.width);
        this.y3 = Math.floor(Math.random() * context.canvas.height);
        this.x4 = Math.floor(Math.random() * context.canvas.width);
        this.y4 = Math.floor(Math.random() * context.canvas.height);
    };
    
    Recta.drawIt = function () {
        context.moveTo(this.x1, this.y1);
        context.lineTo(this.x2, this.y2);
        context.stroke();
    
        context.moveTo(this.x2, this.y2);
        context.lineTo(this.x3, this.y3);
        context.stroke();
    
        context.moveTo(this.x3, this.y3);
        context.lineTo(this.x4, this.y4);
        context.stroke();
    
        context.moveTo(this.x4, this.y4);
        context.lineTo(this.x1, this.y1);
        context.stroke();
    };
  
  
    
<canvas id="canv"></canvas>

1
  • 2
    you try calling drawIt before adding it to Recta.prototype Commented May 8, 2015 at 6:34

2 Answers 2

4

First define the Recta.prototype.drawIt then use it.

 function Recta() {
    this.x1 = Math.floor(Math.random() * context.canvas.width);
    this.y1 = Math.floor(Math.random() * context.canvas.height);
    this.x2 = Math.floor(Math.random() * context.canvas.width);
    this.y2 = Math.floor(Math.random() * context.canvas.height);
    this.x3 = Math.floor(Math.random() * context.canvas.width);
    this.y3 = Math.floor(Math.random() * context.canvas.height);
    this.x4 = Math.floor(Math.random() * context.canvas.width);
    this.y4 = Math.floor(Math.random() * context.canvas.height);
};

Recta.prototype.drawIt = function () {
    context.moveTo(this.x1, this.y1);
    context.lineTo(this.x2, this.y2);
    context.stroke();

    context.moveTo(this.x2, this.y2);
    context.lineTo(this.x3, this.y3);
    context.stroke();

    context.moveTo(this.x3, this.y3);
    context.lineTo(this.x4, this.y4);
    context.stroke();

    context.moveTo(this.x4, this.y4);
    context.lineTo(this.x1, this.y1);
    context.stroke();
};

    var context = document.getElementById("canv").getContext("2d");

for (var k = 0; k < 4; k++) {
    var rect = new Recta();
    rect.drawIt();
}
Sign up to request clarification or add additional context in comments.

2 Comments

you can define function in any place in file and it would be available, but adding drawIt it more setup property in prototype than defining function
@Grundy You are right :) However just to give a clear picture I did that. Edited my answer :)
1

Write the below code at the last (after definition of the object)

    var context = document.getElementById("canv").getContext("2d");

    for (var k = 0; k < 4; k++) {
        var rect = new Recta();
        rect.drawIt();
    }

Comments

Your Answer

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.