Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!hq-usenetpeers.eweka.nl!81.171.88.15.MISMATCH!eweka.nl!lightspeed.eweka.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <3469358.shaKaQI8qd@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 24 Oct 2012 01:15:04 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <1538954.egObkdgl4n@PointedEars.de> <0qGdnW4DceWJZeLNnZ2dnUVZ_gadnZ2d@earthlink.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 86 NNTP-Posting-Date: 24 Oct 2012 02:15:05 CEST NNTP-Posting-Host: c3bb1b53.newsspool3.arcor-online.net X-Trace: DXC=S>jn8\89bHN2:OR3:3gaE@McF=Q^Z^V3H4Fo<]lROoRA8kFK@JjkNj3FBJ;I[YHULT7b]CO X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:16823 Patricia Shanahan wrote: > Jukka K. Korpela wrote: >> A formal parameter has no type. Its values may have types. When you call >> foo(42), then the formal parameter gets the value 42, a number. When you >> call foo('a'), it gets the value 'a', a string. There is no point in >> confusing this by saying that parameters or variables have types. > > So your position is that a variable always has a value, possibly the > undefined value, and the type belongs to the current value. Is that > correct? Your understanding is correct; your understanding of the position is not, because the position is not correct. Like a local variable, a formal parameter only has meaning in the local execution context of the function. There it does have a type: the type of the argument that was passed for it, if any. It is important to know the difference between arguments and (formal) parameters. A function can be passed arguments for which there is no formal (named) parameter; those can be accessed with the arguments object. On the other hand, the type of a parameter for which no argument has been passed is Undefined. Similarly, any variable has a type at runtime: the type of the expression at last assignment (which may be Undefined), or Undefined. If it was not initialized, i. e. had never been assigned a value explicitly, its type was Undefined. In /** * Foos a bar and returns it. * * @param x : Object|String * @param y : Number * @return {String} The fooed bar. */ function foo (x, y) { var z; console.log(x, y, arguments[2], z); return x; } foo(bar, baz, 42); `foo' is the function identifier, and the values of `bar' and `baz' are arguments to the function. `x' and `y' are formal parameters of the function for which the values of `bar' and `baz' have been passed as arguments. A third argument has been passed to the function which is not accessible by name (because there is no parameter for it), but through the `arguments' object; its `2' property specifies that third argument (0-based indexing). The types of `x' and `y' at runtime are the types of the values of `bar' and `baz', respectively. The value of `z' is `undefined', of type Undefined, at runtime *here*, because it would have been never assigned a value explicitly. The *expected* types for those parameters are to be documented, so that the function/method can be used properly. IOW, a user of the function should not expect it to work properly if arguments of an undocumented type had been passed (but they can expect the function to be callable). (AISB, be very careful with overloading.) >> However, it is essential to note that the mechanisms of the language do >> not protect you from "type errors" in the sense as in many other >> languages where you get a compile-time or run-time message about >> assignment of a value of a wrong type. Correct. That follows from the languages using *dynamic* (run-time) typing as opposed to *static* (compile-time) typing. >> Neither do you get automatic type conversions based on the type of a >> variable - simply because variables do not have types in JavaScript. Nonsense. AISB, all variables do have a type. They only do not have a (specific) type at compile time. They do have one *at runtime*. PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee