Applying a Function to Null in Javascript

后端 未结 5 1438
误落风尘
误落风尘 2020-12-03 09:13

Why does the following work:

function sum(a,b) { return a + b; }
var result = sum.call(null,3,4);     // 7

Why is result defined? I am invo

5条回答
  •  渐次进展
    2020-12-03 09:32

    Its not. NULL in this case specifies to what object the this keyword is bound to. In your method, by setting it to NULL it will either not have a this variable binding or it will be bound to the function itself or the window.

    Since you're not using any variables or functions that are accessed via this, then there's no need to use the call method ... you can just use sum(3,4)

提交回复
热议问题