Applying a Function to Null in Javascript

后端 未结 5 1437
误落风尘
误落风尘 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:42

    As the MDN explained.

    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.

    Simply put it by a little code.

    
    

    In the strict mode ,this is null. But in the not strict mode . this is window or global object.

提交回复
热议问题