JavaScript - Objects

0

The console appears "Uncaught ReferenceError: Circle is not defined at scratch.html: 9"

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Objetos</title>
</head>
<body>
    <script>
        c = new Circulo(13, 11, 5);
        document.write('<p>Constructor: <pre>' + c.constructor + '</pre></p>');
    </script>
</body>
</html>

How can I resolve?

    
asked by anonymous 19.04.2018 / 16:02

2 answers

0

You must first define Circulo . At the time you create the instance of Circulo , it is, in that context, undefined.

You must first create your class:

// Criando a classe:
function Circulo(p1, p2, p3) {
  this.p1 = p1;
  this.p2 = p2;
  this.p3 = p3;
  
  // ...
}

// Criando uma instância da classe
var c = new Circulo(1, 2, 3);
console.log(c.constructor);

Note:

If you are using newer versions of JavaScript, you can do this:

// Criando a classe:
class Circulo {
  constructor(p1, p2, p3) {
    this.p1 = p1;
    this.p2 = p2;
    this.p3 = p3;
    
    // ...
  }
}

// Criando uma instância da classe
let c = new Circulo(1, 2, 3);
console.log(c.constructor);
    
19.04.2018 / 16:07
0

You have to define the constructor method before:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Objetos</title>
</head>
<body>
    <script>
        function Circulo (a, b, c) {
           this.a = a;
           this.b = b;
           this.c = c;
        }
        var c = new Circulo(13, 11, 5);
        document.write('<p>Constructor: <pre>' + c.constructor + '</pre></p>');
    </script>
</body>
</html>
    
19.04.2018 / 16:18