Problem making a method call in class constructor (javascript)

Asked

Viewed 58 times

0

I am learning javascript and am getting the following error: setTitulo is not defined

Why does this error happen? The setTitulo method should not be set since it belongs to the class?

class Nota{
    constructor(titulo, descricao){
        try {
            this.titulo = setTitulo(titulo);
            this.descricao = descricao;            
        } catch (error) {
            showError(error);
        }        
    }

    setTitulo(titulo){
        if(titulo !== null && titulo.length > 0)
            return titulo;
        else
            throw new Error("title", "Title is sadsadsa");        
    }  

}



class Error{
    constructor(name, message){
        this.name = name;
        this.message = message;
    }    
}

const showError = function(error){
    const panelError = document.getElementById("panel-attention");
    panelError.innerHTML = `Error: ${error.message}`;
}
  • 4

    Have you tried this.setTitulo(titulo);?

1 answer

2


As @Now-Now commented, if you are accessing a member of your own class, you should use this.

To better understand, I’ll even use your example, in the method setTitulo. Supposedly by name, he should set the "title", but he’s not doing it. If he changes to it, he’ll have a conflict with "title":

setTitulo(titulo){
        if(titulo !== null && titulo.length > 0) {
            titulo = titulo;  //aqui está o problema
            return titulo;
        }
        else
            throw new Error("title", "Title is sadsadsa");        
    }  

Note that in order to pass the value received in the "title" parameter to the inner member "title" that was set in the constructor, you need to use this.titulo = titulo. Similarly, you need to use to inform that you are a member of the class itself, if not the interpreter of javascript will fetch outside the class, and as there is, in case of your mistake, setTitulo, makes that mistake.

See the changed code:

class Nota{
   
    constructor(titulo, descricao){
        try {
            this.titulo = this.setTitulo(titulo);
            this.descricao = descricao;            
        } catch (error) {
            showError(error);
        }        
    }

    setTitulo(titulo){
        if(titulo !== null && titulo.length > 0) {
            this.titulo = titulo;
            return titulo;
        }
        else
            throw new Error("title", "Title is sadsadsa");        
    }  
    
    getTitulo() {
      return this.titulo;
    }

}

class Error{
    constructor(name, message){
        this.name = name;
        this.message = message;
    }    
}

const showError = function(error){
    const panelError = document.getElementById("panel-attention");
    panelError.innerHTML = `Error: ${error.message}`;
}

var nota = new Nota("tit", "descr");
console.log(nota.getTitulo());
nota.setTitulo("Titulo alterado");
console.log(nota.getTitulo());
nota.setTitulo("");
<div id="panel-attention"></div>

  • 1

    Apparently there is a problem, setTitulo is an assignment method and returns undefined, then it shouldn’t be this.titulo = this.setTitulo(titulo);. It should only be this.setTitulo(titulo);.

  • Yeah, like I was using the setTitulo to set the property, was missing the Return, thanks for commenting ;)

Browser other questions tagged

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