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

后端 未结 4 1299
小鲜肉
小鲜肉 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:58

    You wouldn't have to modify Function.prototype, but Number.prototype. You're trying to create a new method that acts on a number, not on a function. This does what you're trying to do:

    Number.prototype.square = function() {
      return this * this;
    }
    
    let x = 4;
    
    let y = x.square().square(); // -> 256
    

提交回复
热议问题