6

I often see that when a function needs to be called with bound parameters in no particular context the undefined is more often than not is preferred over the null as a choice of context, as in:

f.call(undefined, param1, param2)

is preferred over:

f.call(null, param1, param2)

I'm wondering if there is any particular reason for this?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Preference. It is just a matter of taste. – Aadit M Shah Apr 17 '15 at 17:52
  • For more details: http://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript AND http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html – The Guest Apr 17 '15 at 17:54
  • null means there is an object with no value, while undefined means there is nothing at all. This concept only exists in js, as far as I know. – mondjunge Apr 17 '15 at 17:55

2 Answers2

6

What's the benefit of binding to undefined instead of null?

I don't think there is any. From 10.4.3 Entering Function Code:

  1. If the function code is strict code, set the ThisBinding to thisArg.
  2. Else if thisArg is null or undefined, set the ThisBinding to the global object.
  3. ...

So, if the code is not strict, in both cases this will be set to the global object.

But if the code is strict, this will actually either be null or undefined. f could be implemented to distinguish these cases, but that doesn't seem to be a very likely scenario to me.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    I see, thanks. So I might as well use `null` as I've done before since I treat `undefined` as something I haven't touched and try to avoid it in my applications, whereas `null` signifies that the value has been set on purpose. As I'm calling the function I am setting the context on purpose. – Max Koretskyi Apr 17 '15 at 18:03
  • 1
    Sounds very reasonable to me! (that's how I do it as well) – Felix Kling Apr 17 '15 at 18:03
  • 1
    Just for the sake of completness, an excract from ecma script third edition http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf `The caller provides the this value. If the this value provided by the caller is not an object (including the case where it is null), then the this value is the global object` – Olga Apr 17 '15 at 18:07
1

In javascript, undefined extends past the scenario where a value has not been set. For instance, if you are looking for a property on a model that does not exist, it will yield undefined.

Its not that odd either to have code like this:

var k;

//do something (possibly setting k)

alert(k);

If the value has not been set, it will be undefined rather than null.

Long story short, it is still a preference, but by using undefined you are more likely to catch the cases where values have not been initialized or try accessing properties of objects that do no exist.

Not sure this answers your question.

tribe84
  • 5,432
  • 5
  • 28
  • 30
  • That doesn't really explain why one would use the other in the context of `.call`. – Felix Kling Apr 17 '15 at 17:59
  • 1
    thanks, but it doesn't answer my question as I'm quite good at distinguishing `null` from `undefined` :) – Max Koretskyi Apr 17 '15 at 17:59
  • I agree that it's more in concept. The idea might be that since a bound function cannot be given any value for "this" (and 'null' is actually a value per se), that 'undefined' makes more sense (sort of "no value at all, since no value matters" [assuming null as a value for no object reference, as opposed to no valid value]). – James Wilkins Apr 17 '15 at 18:00