Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "J.R." Newsgroups: comp.lang.javascript Subject: Re: Language Confusions Date: Thu, 24 Nov 2011 00:07:46 -0200 Organization: Aioe.org NNTP Server Lines: 48 Message-ID: References: NNTP-Posting-Host: edb3lPNLwDIT/BKKc/Xuzw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:8.0) Gecko/20111105 Thunderbird/8.0 X-Antivirus-Status: Clean X-Notice: Filtered by postfilter v. 0.8.2 X-Antivirus: avast! (VPS 111123-2, 11/23/2011), Outbound message Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:8586 On 23/11/2011 23:38, Gene Wirchenko wrote: > Dear JavaScripters: > > I have been carefully studying JavaScript and have learned a lot > more than I ever knew about it. I have some questions about niggling > points: > > 1) What is the difference between: > s="Hello"; > s=new String("Hello"); > s=String("Hello"); > The text I am studying says that the first two are the same. Fine. > The third behaves the same way AFAICS. Could someone please > illustrate the difference? > If you paste the following code snippet in a JavaScript console such as Firebug's (in Firefox), you will notice the difference: var s1 = "Hello", s2 = new String("Hello"), s3 = String("Hello"); console.log(typeof s1); // "string" console.log(typeof s2); // "object" console.log(typeof s3); // "string" So "the first two are the same" is wrong... Check the ECMAScript Language Specification to learn the why's. > 2) I am unclear on exactly all what .prototype does. For adding > methods to an object type, fine. In the text that I am studying, > there is an example of a table object. The author then shows how to > add a method to an object type (without modifying the base code), and > how it is done has me puzzled: > function table_colorWrite(doc) > body omitted > followed by > table.prototype.colorWrite=table_colorWrite > I am not clear on what .prototype does in this line. I think I must > be missing something about the JavaScript object model. An explanation about prototype in JavaScript would require a lot of typing here... So I'd suggest a further reading at -- Joao Rodrigues (J.R.)