Javascript - Objects

Asked

Viewed 31 times

0

On the console appears "Uncaught Referenceerror: Circle is not defined at draft.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 solve?

2 answers

0


You must set first Circulo. The moment you create the instance of Circulo, he 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 so:

// 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);

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.