Error in ES6: "is not a function"

2

How to call a method (from another) that are in the same class in NodeJS?

class Usuario{  

    static foo(){  
        let a = this.bar();  
    }    

    bar(){  
        return true;  
    }

}


Usuario.foo();

I try this way and return:

  

TypeError: this.bar is not a function

What is wrong? How can I solve it?

    
asked by Alvaro Montoro 10.04.2017 в 22:36
source

4 answers

3

The problem is that you are trying to access a non-static method from a static method. Static methods are called without instantiating their class (they belong to the class itself), while non-static methods need an object (the class has been instantiated).

The solution to the problem will depend on the use you are going to make of foo() and bar() . For example, you could make bar also static:

class Usuario{  

  
    static foo(){  
        let a = this.bar();  
        console.log(a);
    }    

    static bar(){  
        return true;  
    }

}


Usuario.foo();
    
answered by 10.04.2017 / 22:52
source
0

According to the documentation:

Class body and definition of methods Edit

The body of a class is the part that is between the braces {}. This is the place where class members are defined, such as methods or constructors. Strict mode

The body of class declarations and class expressions are executed in strict mode. Builder

The constructor method is a special method to create and initialize an object created with a class. There can only be one special method with the name "constructor" in a class. If it contains more than one occurrence of the constructor method, an Error SyntaxError will be thrown

A constructor can use the super reserved word to call the constructor of a superclass Prototype methods

documentation link

Example with your code:

class Usuario{  

get foo(){  
    let a = this.bar();  
}    

bar(){ 
        console.log('si')
    return true;  
}

}

usr = new Usuario
usr.foo;

class Usuario{  

    get foo(){  
        let a = this.bar();  
    }    

    bar(){ 
    		console.log('Método bar')
        return true;  
    }

}

usr = new Usuario
usr.foo;
    
answered by 10.04.2017 в 22:52
0

As a colleague mentioned above, you must add static to the bar function, since foo() is within a static context you should call functions that are so too.

class Usuario{  

    static foo(){  
        let a = this.bar(); 
        console.log(a);
    }    

    static bar(){  
        return true;  
    }

}

class UsuarioSinStatic {
  foo() {
  	let a = this.bar();
        console.log(a);
  }
  
  bar() {
  	return true;
  }
}


// Clase estatica
Usuario.foo();

// Clase no estatica
var userSinStatic = new UsuarioSinStatic();
userSinStatic.foo();

For more information about the methods and static attributes I leave this documentation that I hope that is useful to you.

Greetings!

    
answered by 10.04.2017 в 22:54
0

You can not use this in a static method. This concept is also applied in languages such as Java. When you declare a static method in an class ES6 that, in passing, is a function with prototype (as always), the method is associated with the constructor and not with the prototype as expected. That's why within a static method you do not have access to the context of the class .

The correct thing to do is to call static properties and methods using the constructor directly:

Usuario.bar();
    
answered by 10.04.2017 в 22:54