Is there a reason why `this` is nullified in Crockford's `curry` method?

后端 未结 4 632
遇见更好的自我
遇见更好的自我 2021-01-31 20:18

In Douglas Crockford\'s book \"Javascript: The Good Parts\" he provides code for a curry method which takes a function and arguments and returns that function with

4条回答
  •  遇见更好的自我
    2021-01-31 20:45

    But what I want to know is why does he set this to null?

    There is not really a reason. Probably he wanted to simplify, and most functions that make sense to be curried or partially applied are not OOP-methods that use this. In a more functional style the history array that is appended to would be another parameter of the function (and maybe even a return value).

    Wouldn't the expected behavior be that the curried method is the same as the original, including the same this?

    Yes, your implementation makes much more sense, however one might not expect that a partially applied function still needs to be called in the correct context (as you do by re-assigning it to your object) if it uses one.

    For those, you might have a look at the bind method of Function objects for partial application including a specific this-value.

提交回复
热议问题