How is this function executed?

recently read ide" rel=" nofollow noreferrer "> functional programming Guide

I can"t understand the part of the container. There is a code that says Functor as follows:

class Maybe {
  static of(x) {
    console.log(1);
    return new Maybe(x);
  }

  get isNothing() {
    console.log(2);
    return this.$value === null || this.$value === undefined;
  }

  constructor(x) {
    console.log(3);
    this.$value = x;
  }

  map(fn) {
    console.log(4);
    return this.isNothing ? this : Maybe.of(fn(this.$value));
  }

  inspect() {
    console.log(5);
    return this.isNothing ? "Nothing" : `Just(${this.$value})`;
  }
}

var test1 = Maybe.of("Malkovich Malkovich");
console.log(test1);

var test2 = Maybe.of(null).map(_.match(/a/ig));
console.log(test2);

var test3 = Maybe.of({ name: "Boris" })
                  .map(_.prop("age"))
                  .map(_.add(10));
console.log(test3);

var test4 = Maybe.of({ name: "Boris", age: 14 })
                  .map(_.prop("age"))
                  .map(_.add(10));
console.log(test4);

what is strange and incomprehensible is how the inspect function is executed? There seems to be no place to call this function.
I hope there is a great god who can answer and communicate with me.


inspect is only used to check the values in the container. You do not call it here, and of course you will not execute

.
Menu