Is there any way to make a function's return accessible via a property?

后端 未结 4 1247
小鲜肉
小鲜肉 2021-01-27 03:30

I\'m a JS dev, experimenting with functional programming ideas, and I\'m wondering if there\'s anyway to use chains for synchronous functions in the way the promise chains are w

4条回答
  •  独厮守ぢ
    2021-01-27 03:55

    You can set square and num as a property of square call`

    function square (num) {
    
      if (!this.square) {
        this.square = square;
        this.num = num || 0;
      };
      
      if (num === undefined) {
        this.num *= this.num
      }
      else if (!isNaN(num)) {  
       this.num *= num;
      };
      return this;
      
    }
    
    let foo = 2;
    let c = new square(foo).square().square();
    console.log(c.num);

提交回复
热议问题