鍍金池/ 問答/C++  HTML/ 這句函數(shù)是怎么被執(zhí)行的?

這句函數(shù)是怎么被執(zhí)行的?

最近在看函數(shù)式編程指南

看到容器的部分有點看不懂,其中有一段代碼講Functor的如下:

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);

比較奇怪和不理解的是inspect函數(shù)是怎么執(zhí)行的?似乎沒有地方調(diào)用這個函數(shù)呀。
希望有大神可以解答交流一下~

回答
編輯回答
憶往昔

inspect只是為了查看容器里面的值的時候用的,你這里沒有調(diào)用,當(dāng)然也不會執(zhí)行

2018年2月16日 14:27