-
-
Save rwaldron/2926029 to your computer and use it in GitHub Desktop.
// http://traceur-compiler.googlecode.com/git/demo/repl.html | |
// open the console | |
// paste this into the text area. | |
// | |
// or | |
// | |
// Run in Firefox 15 (Nightly) | |
function nullIsNotUndefined( arg = "foo" ) { | |
return arg; | |
} | |
console.log( nullIsNotUndefined() === "foo" ); | |
console.log( nullIsNotUndefined(null) === null ); |
They check for only undefined
; so in the first case arg
is undefined so therefore is assigned "foo". In the second case, null
is a valid value and therefore accepted as if it were defined intentionally.
Hmm...that seems a bit inconsistent (String() === ""; String(undefined) === "undefined"
), but I guess it makes sense.
Thank you!
@rwldrn: in the current Firefox implementation, nullIsNotUndefined(undefined)
returns undefined
.
The proposal says: "If no actual parameter was passed", then use the default parameter.
http://wiki.ecmascript.org/doku.php?id=harmony:parameter_default_values
Of course, that is something that Traceur would have a hard time implementing.
That makes sense though, because it's explicit and there would be something at arguments[0]
, right? undefined
is a thing:
15.1.1.3 undefined
The value of undefined is undefined (see 8.1). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
...
8.1 The Undefined Type
The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.
...
4.3.9
undefined value
primitive value used when a variable has not been assigned a value.
...
4.3.10
Undefined type
type whose sole value is the undefined value.
Unrelated: I had tested this in FF15 - which I thought was the latest Nightly, I was wrong, the latest Nightly is 16x
It does make sense, but it also means that the difference between undefined and null is only semantic.
(That, and the fact that you cannot assign null
.)
Technically, undefined
isn't writable or configurable either, but unfortunately implementation adherence isn't consistent
The end result is, undefined
and null
have been spec'ed as having very similar behavior. Their main difference is in their meaning. The standard library uses null
very sparingly, but quite frankly each of its use could be replaced by undefined
without losing anything.
Not at my computer at the moment (and running Traceur on an iPod touch is an unenviable experience), but what does
nullIsNotUndefined(undefined)
return? In other words, do default parameters check forundefined
, orarguments.length
?