Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: "Jukka K. Korpela" Newsgroups: comp.lang.javascript Subject: Re: Programming style question Date: Thu, 18 Oct 2012 15:32:39 +0300 Organization: A noiseless patient Spider Lines: 46 Message-ID: References: <1538954.egObkdgl4n@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 18 Oct 2012 12:32:39 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="9e0ae8574c58208bf47738e1c9986fe4"; logging-data="13560"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+d3pBePXwvY5aJ0IA9mRxf" User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 In-Reply-To: X-Antivirus-Status: Clean X-Antivirus: avast! (VPS 121018-0, 18.10.2012), Outbound message Cancel-Lock: sha1:C0G5kF/t0Zwyo4+ONwISmDujEnQ= Xref: csiph.com comp.lang.javascript:16707 2012-10-18 15:06, Patricia Shanahan wrote: > Thomas 'PointedEars' Lahn wrote: > >> No. (However, contrary to common misconception, there are no "untyped >> variables". Implementations of ECMAScript Ed. 1 to 3, 5, and 5.1 are >> dynamically and weakly typed, not untyped. Uninitialized variables >> and trailing parameters for which no argument has been passed, hold >> the `undefined' value, the sole value of the Undefined type.) That's misleading play with words. Calling a variable typed just because its values have types is pointless. > As I understand the issue, a variable always has a type, but that type > can change on assignment. No, a variable has values (possible different values, possibly of different types, at different times, and the values have types. > Similarly, the type of a formal parameter is > determined by the type of the actual parameter in the call 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. As a programming style matter, you can decide to use each variable for values of a single type only. You can say that in a comment, or maybe just implicitly in initialization, e.g. var boo = '' would mean (in "typed style") that you intend to use boo with a string as its value. I don't see any general problem with such an approach. It's mostly a good principle to use one variable for values of one type - one might even say that it's probably worth a comment in the source if you do otherwise in some case. 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. Neither do you get automatic type conversions based on the type of a variable - simply because variables do not have types in JavaScript. -- Yucca, http://www.cs.tut.fi/~jkorpela/