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 invoking sum as a method of null. But null is not an object and cannot have properties!
What is going on?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first argument for
Function.prototype.callis the context, which defines thethisvalue for the execution context of the invoked function, nothing else.So basically, you’re saying that
thisis referring tonull(at least, in ES5 strict mode), but since you don’t accessthisanyway, it makes no difference.In non-strict mode,
thiscannot benull, so it’s replaced with the global object instead.