JavaScript 'is not a function' - object instance -


i error:

uncaught typeerror: rect.drawit not function

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

  var context = document.getelementbyid("canv").getcontext("2d");            (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>

first define recta.prototype.drawit 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");  (var k = 0; k < 4; k++) {     var rect = new recta();     rect.drawit(); } 

Comments