Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
TypeError no detectado: this.canvas no está definido

Estoy tratando de crear una instancia de la clase Object pero recibo este error:
TypeError no detectado: this.canvas no está definido

Soy bastante nuevo en javascript, por lo que podría haber cometido algunos errores tontos.

Aquí está el código y la estructura de carpetas:

 projectname/ - index.html - index.js - js/ -- Object.js

archivo: index.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>title</title> </head> <body> <div id="container"></div> <script src="index.js" type="module"></script> </body> </html>

archivo: index.js

 import { Object } from "/js/Object.js"; var obj = new Object(); obj.position = (500, 500, 0);

archivo: /js/Objeto.js

 export class Object { constructor() { this.position = new Array(0, 0, 0); this.velocity = new Array(0, 0, 0); this.force = new Array(0, 0, 0); this.mass = 0; this.canvas = document.createElement("canvas"); } get position() { return this._position; } set position(n_position) { this._position = n_position; this.canvas.style.left = n_position[0].toString()+"px"; this.canvas.style.top = n_position[1].toString()+"px"; } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Dentro de tu constructor tienes:

 this.position = new Array(0, 0, 0);

que llama a la función setter que intenta leer this.canvas antes de que tengas:

 this.canvas = document.createElement("canvas");

que crea el lienzo que intentaste modificar cinco líneas antes.


El orden importa. Cree su lienzo antes de intentar modificarlo.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!