Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Can somebody explain this to me Date: Tue, 06 Jan 2015 17:26:24 +0100 Organization: PointedEars Software (PES) Lines: 99 Message-ID: <1761561.NWXpXnK3hu@PointedEars.de> References: <1590597.KOVAYohEvS@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1420561608 4336 eJwFwQEBwDAIAzBLdEDZ7ACvfwlP0glOBZORSoW/rv4MCpPdGtmSc3ZdDYR2zk0bAPl47Acd6RC+ (6 Jan 2015 16:26:48 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 6 Jan 2015 16:26:48 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwYEBACAEBMCVyHs1Tsj+I3TnRmUF6ISPT6i9dZ5lCyofeHNFV4wldpTKLUdjIHumRHqdNt0pRQiJD2KHFYA= Cancel-Lock: sha1:WK7O/TOTw/28fjVoBLfaJw8jj8c= X-NNTP-Posting-Host: eJwNwYEBABAMA7CX1LZ2zmH4/wSSMIIlZ9Dj/meilO5QS53FgUWif4SNXmF37szckpvXA/88D+0= Xref: csiph.com comp.lang.javascript:26468 John Harris wrote: > Thomas 'PointedEars' Lahn wrote: >> Tony Johansson wrote: >>> The code at the end works but I have a question. >>> When I have this code construction with prototype I can call the getFoo >>> in >>> this way var poo = f.getFoo(); // "bar" >>> P.prototype.getFoo = function () >>> { >> >>I recommend to put the opening brace of a function expression on the same >>line as the “function” keyword in order to tell function declarations and >>function expressions apart more easily. > > And I recommend that you don't change, though a smaller indent > wouldn't hurt. Having the curly brackets lined up makes it easier to > see where the code body begins and ends, especially when the body > contains inner function definitions. However, as you have provably little if any theoretical knowledge of and practical experience with the programming languages in question, your recommendations should be weighed against that. There is an important difference between function declarations and function expressions that is _not_ syntactical, and it is wise to write source code so that such differences are easier to see. >>> return this.foo; >>> }; >>> >>> >>> I mean if I add this piece of code into the class definition >> >>There are no classes in this case; > > Not true. See an earlier thread. In the earlier thread you already failed to provide convincing, let alone non-fallacious arguments that the terminology you prefer, in the way you prefer it, is useful in a technical discussion on the subjects that are on- topic here. >>prototype-based inheritance is used. > >>> I can call the getFoo in this way var poo = f.getFoo(); // "bar" >> ^ method >> >>> this.getFoo = function () >>> { >>> return this.foo; >>> }; >>> >>> So as a summary can somebody explain what is the advantage to use a >>> prototype in this exemple? >> >>Inherited methods can be reused. In this example, one advantage is that >>only one Function instance is created, which saves memory as compared to >>the instance method approach. It also saves runtime because the creation >>of that object only happens once. (There are also disadvantages.) >> >>> function P(foo) //Here is the class definition > > > No; here is the class's constructor function. Utter nonsense. > The class's definition has to be deduced from the code, There is no class as there are no classes in the respective original language (what is created from that by compilers for virtual machines for optimization, indeed, is irrelevant in that regard). Therefore there is no “class definition” to “deduce” other than that which can only be ascribed to either too vivid imagination or utter wannabe-ness. The constructor function’s declaration, on the other hand, is plain to see, it can be looked up in authoritative reference material, and it is what this code is being parsed as by *all* relevant script engines. > which is presumably why some people say ECMAScript, up to v5 at least, has > no classes. ECMAScript Editions 1 to 3, and 5.x, simply do not; neither have their implementations. (The abandoned Edition 4 working draft is irrelevant with regard to client-side code in Web browsers, but it must be excluded from that list.) However, ECMAScript Edition 6 and its implementations are going to, and they are going to have prototype-based inheritance as well. Therefore it is not useful to call something a class that is not called a class in any authoritative reference material. Please stop confusing people who seek clarity here. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.