9

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?

0

5 Answers 5

18

The first argument for Function.prototype.call is the context, which defines the this value for the execution context of the invoked function, nothing else.

So basically, you're saying that this is referring to null (at least, in ES5 strict mode), but since you don't access this anyway, it makes no difference.

In non-strict mode, this cannot be null, so it's replaced with the global object instead.

Sign up to request clarification or add additional context in comments.

5 Comments

this is not context, it's a component of an execution context.
@RobG: its probably misleading the way I described it. However, this will refer to the context/scope object for the current execution context.
jAndy—it's impossible to reference a scope object (other than the limited facility provided by with and the special case of a global object), it's expressly forbbiden by ECMA-262 (See §10.2 Lexical Environments, last sentence). And this is one parameter that is set as a component of an execution context (see §10.3 Execution Contexts), where it also says it is impossible to access an execution context.
@RobG: oh come one man, I now about this language and I don't need to read all those links again. Whats wrong with you ? I'm not talking about the specification and implementation of "scope" and context objects. It should be pretty obvious what I mean with "context" here.
however, I don't want to induce a flamewar here. I guess you just want to be a little picky, thats just fine. But you and everybody who reads this should be able to identify what I'm talking about, and that is ecmascript-objects which are referred by this. Its pretty pretty common to call that "context", even if the spec has of course a different meaning of that.
12

When you use .call or .apply with null or undefined, the default this (usually window) is automatically used instead if not in strict mode.

From MDN (emphasis mine):

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.

If you are in strict mode, it actually will be null or undefined.

(function() {
    'use strict';

    return this;
}).call(null); // null
(function() {
    'use strict';

    return this;
})(); // undefined

5 Comments

I dont think you quoted MDN correctly, look at Alfabravo's answer.
@Fawkes5: Maybe if you don't think I quoted MDN correctly, you should follow the link to MDN I provided and see for yourself that it's exactly the same? The only differences between Alfabravo's answer and mine are "fun" (the original text) and the emphasis (which I already said was mine).
i know but what's with the thisArg, and 'fun' in the first sentence
@Fawkes5: fun is the name of the function. thisArg is the name of the first argument to .call. (Again - see the documentation.)
:Ah i see. Regardless its out of context.
4

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)

Comments

1

As stated in, for example, MDN, the first argument is

The value of this provided for the call to [the method]. 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.

Comments

-1

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.

<script >
        'use strict'
        function fun() {
            alert(this);
        }
        fun.call(null);

    </script>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.