Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: "Christoph M. Becker" Newsgroups: comp.lang.javascript Subject: Re: ECMAScript and classes Date: Fri, 24 Oct 2014 19:45:09 +0200 Organization: solani.org Lines: 66 Message-ID: References: <4fak4ap1hljij9ea0ae6i2bamg02n6fk0u@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: solani.org 1414172697 29215 eJwFwYEBACAEBMCVSHjj6Mv+I3TnFhrMHR7bx0fidrFWzlRfPacFsCG5zHAfmAmAO+lPRD8vdRFw (24 Oct 2014 17:44:57 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Fri, 24 Oct 2014 17:44:57 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 X-User-ID: eJwNysEBwCAIA8CVQAzIOAjN/iPYex/M1Tu2wzcIKlV9VjFyjXxMMzHdfes/ZyAHMt3Mm4XIeBUoENk= In-Reply-To: <4fak4ap1hljij9ea0ae6i2bamg02n6fk0u@4ax.com> Cancel-Lock: sha1:nhQrGY6BW+WIW6rRZs15Y8zAo+k= X-NNTP-Posting-Host: eJwFwYEBwDAEBMCVePFiHCX2H6F3blR2HDqPr68o7qRmEoba6TsxL7LNBUh5GISVqOpXNP4HXRA2 Xref: csiph.com comp.lang.javascript:25983 John Harris wrote: > On Thu, 23 Oct 2014 18:37:40 +0200, "Christoph M. Becker" > wrote: > >> Usually you wouldn't seal the class instances, though, but rather >> prevent their extension and maybe remove the configurable attribute on >> the individual properties. > > But that's what seal does! Of course, you're right. >> The "Poor Old Programmer" could, however, write a function that does all >> this for him. > > And then remember to write the code in the constructor to call the > function. No, I meant something like function defineClass(...) {...} There are numerous variants available. > Multiple constructors, on the other hand, give you a choice of > parameters. You can already have new Thing() and new Thing(3,4) by > putting code in the constructor to look at the argument list and then > replacing missing arguments by default values : oh look, multiple > constructors. Well, as ECMAScript function signatures are quite flexible, you don't necessarily need multiple constructors, and you won't have any improved type safety by having them. > Occasionally, though, you would like the same number of parameters of > the same type with different meanings. E.g A new complex number given > the (x,y) values or the (modulus,angle) values. You could create functions as methods of the constructor ("static methods"), each of them converting the arguments as needed and calling the constructor with the new operator, for instance: Complex.createCartesian = function (x, y) {...} Complex.createPolar = function (modulus, angle) {...} > Full support would include creating the new object's properties, > presumably holding a default value such as undefined. This would also > mean that the property names are listed in the class description, as > they should be. I agree that this would be nicer, but you could use a class defining function to have a similar notation. > And wouldn't it be nice if you could write x instead of this.x inside > methods. AFAIK, the need to qualify instance properties is quite common in "dynamic" languages. And at least it has also some advantages. Note, that I do not argue that support for classes in ECMAScript shouldn't be improved, but rather that one has to take what he gets, and that there are viable ways to deal with the limitations (at least in ES 5). -- Christoph M. Becker