Javascript sub-functions

Asked

Viewed 86 times

0

How do I create sub-calls in Javascript functions?

Below follows example of usage, my doubt is how to create a Function of this type. Depending on what happens within the "Travel" routine, a call to "Arrival" and/or "Problem"

this.Viajar('são paulo').Chegada(() => {
    console.log('chegou com sucesso')
}).Problema(() => {
    console.log('pneu furou')
});
  • an if does not solve? I did not understand your question right

  • 2

    You can use Promise. https://codepen.io/valdeir2000/pen/WJJZMQ?editors=1011

1 answer

1


What you are referring to are not actually functions, are classes, where classes have methods within 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 pre-defined functions to handle your elements. Check out other applications on: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

  • Right, but I need to fire an external method through another internal method, in your example it would be something like Point.distance(P1, P2). Return((msg) => { console.log('return was: ' + msg; })

Browser other questions tagged

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