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

后端 未结 4 664
遇见更好的自我
遇见更好的自我 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:44

    From MDN:

    thisArg The value of this provided for the call to fun. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.

    Hence, if the method is in non-strict mode and the first argument is null or undefined, this inside of that method will reference Window. In strict mode, this is null or undefined. I've added a live example on this Fiddle.

    Furthermore passing in nullor undefined does not do any harm in case the function does not reference this at all. That's probably why Crockford used null in his example, to not overcomplicate things.

提交回复
热议问题