Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: "undefined"? Date: Thu, 07 Jan 2016 04:20:56 +0100 Organization: PointedEars Software (PES) Lines: 107 Message-ID: <7302001.AtPqRxjPWK@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1452136857 22082 eJwVysERACEIBLCWQNhVygGV/kvwbvINjMo9naCjf1q27tBcuL7ZGhKVX0mPZuU4wCWGTTE5eB0nENE= (7 Jan 2016 03:20:57 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 7 Jan 2016 03:20:57 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwQkRwEAIBDBLvMsgB66sfwlN0qF4FUhEMtk0ac4pcR2tyRn/eKdFQ7qaSkFKd+K9ON/uw+Y4RdT2B1x1FYc= Cancel-Lock: sha1:iI63gOHVTCgEtV5/qedu5WKr+FU= X-NNTP-Posting-Host: eJwFwQERACAIA8BKKGxIHMFb/wj+w7k4GQQDgo7f8rCX71YmYDZrcj+y5AV1blmf5khl0AcLxhDo Xref: csiph.com comp.lang.javascript:29157 Stefan Ram wrote: > I remember the advice: "Don't assign »undefined« > to something, only use it for testing (as in > »... === undefined«)." > > But I forgot the reason. > > Could it be that we do not want to write > > .property = undefined > > so that the /existing/ property with the value > »undefined« is not mistaken for a property that > does not exist? And we do not want to write That can be one reason. Another reason is that “undefined” (different to “null”, “true” and “false”) is actually a property of the global object (IMHO a mistake in language design) that had not been read-only in the past. However, the latter reason also casts into doubt the recommendation to compare strictly against “undefined”. So when backwards compatibility is important (in my work it always is), I recommend to compare “typeof … == "undefined"” instead of “… === undefined”. JSHint begs to differ by default because of “==”, but can be convinced with { "eqeqeq": false } in the project’s .jshintrc (you can, of course, also skip this and use “===” at slightly reduced backwards compatibility; I am presently not certain if the implementation versions that had a writable “undefined” property directly correspond with those that did not yet support the “===” operator). [If you never tried Atom with preinstalled “language-javascript” and post- installed “linter-jshint” package, I strongly recommend that you do that soon. After 10 years of preferring Eclipse, I am in the process of switching from Eclipse to Atom, and that *fast* validation-as-you-type is one important reason for it.] > function() { ... return undefined; } > > so that this function that returns something > which happens to be the value »undefined« is > not mistaken for a function that does not > return anything at all? A function always returns a value. If it does not return a value explicitly, it returns the initial value of the “undefined” property. Therefore, return undefined; is better written return; not least for the reasons given above. > Which of the following three fragments should > be preferred, and why? > > square_root(x) { if( x < 0 )return NaN; ... } Of the posted variants, this one is to be preferred because the function would return a value of type Number if it succeeded. The number of different types that a function returns should be minimized. However, since the function would have been called with an unsupported argument, it makes more sense to throw an exception or do not do any preior checks if an exception would be thrown by the operation anyway *and* if from the latter exception message it would be obvious to the caller what they did wrong. One must resist the temptation to make it too comfortable for the caller because that will actually cause greater trouble for all later, like excessive overloading to make it “easy” to use a function. > square_root(x) { if( x < 0 )return null; ... } This variant should be avoided because “null” indicates that otherwise an object reference would be returned, which it actually would not with a square root function. > square_root(x) { if( x < 0 )return undefined; ... } Also to be avoided in this case, but also see above. > (actually, the word »undefined« best expresses the > mathematical way of wording, which says that the > square root of -1 is not defined [in the reals].) But: 1/0 === Infinity (It is unwise to ask for opinions and at the same make it clear that one has already formed an opinion.) Goodnight ;-) -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.