Javascript SubFunctions

0

How do I create sub-calls in Javascript functions?

Below is an example of use, my question is how to create such a function. Depending on what happens inside the "Travel" routine, the call is called "Arrival" and / or "Problem"

this.Viajar('são paulo').Chegada(() => {
    console.log('chegou com sucesso')
}).Problema(() => {
    console.log('pneu furou')
});
    
asked by anonymous 11.05.2018 / 18:22

1 answer

0

What you are referring to are not really functions, they are classes, where classes have methods inside them, see below:

class Ponto {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    static distancia(a, b) {
        const dx = a.x - b.x;
        const dy = a.y - b.y;

        return Math.sqrt(dx*dx + dy*dy);
    }
}

const p1 = new Ponto(5, 5);
const p2 = new Ponto(10, 10);

console.log(Ponto.distancia(p1, p2));

You can create classes to manipulate values, with predefined functions to handle their elements. Check out other apps at: link

    
11.05.2018 / 18:48