From riegel at clearimageonline.com Sat Oct 3 13:36:45 2009 From: riegel at clearimageonline.com (Terry Riegel) Date: Sat, 3 Oct 2009 14:36:45 -0400 Subject: [Javascript] Hopefully quick question. Message-ID: I have this construct... Which means when a person clicks on obj the code in that function will fire, which will alert the LAST value of obj. I want to pass that function the value of obj at the time it is set up. I understand how this code works now, what I don't understand is how I would make it work the other way. Any ideas? Terry From david at dorward.me.uk Sat Oct 3 14:03:58 2009 From: david at dorward.me.uk (David Dorward) Date: Sat, 3 Oct 2009 20:03:58 +0100 Subject: [Javascript] Hopefully quick question. In-Reply-To: References: Message-ID: <145CC312-FAE1-4EC7-A33A-18B046012467@dorward.me.uk> On 3 Oct 2009, at 19:36, Terry Riegel wrote: > I have this construct... > > So you want a click event on 'myotherdiv' to alert 'mydiv'? It might help if you explain your use case more clearly. -- Hassan Schroeder ----------------------------- hassan at webtuitive.com webtuitive design === (+1) 408-621-3445 === http://webtuitive.com twitter: @hassan dream. code. From scott at randomchaos.com Sun Oct 4 21:19:50 2009 From: scott at randomchaos.com (Scott Reynen) Date: Sun, 4 Oct 2009 20:19:50 -0600 Subject: [Javascript] Hopefully quick question. In-Reply-To: References: Message-ID: On Oct 4, 2009, at 6:03 PM, Troy III Ajnej wrote: > Not counting but, there's been 8 years or so, that I've been listening > to all kinds of morons yelling STANDARDS; W3C; "how it shoould be" > "document.getElementById('ID') - IS BETTER" etc etc etc... > > I mean, why is there, all of a sudden, this sort of need for coding > convenience when there was a beautifully nice, clean, simple and > ultra-convenient mydiv call and super-widely supported also; > Just like super-convenient innerHTML is, and has been for little over > a decade or so? Framework convenience functions are generally based on standards. For example, if you look at the jQuery source, you'll see that ID-only selectors like the one that sparked this thread are passed directly to document.getElementById(). Of course, the same function that does that can also do selection by class name, tag name, attribute values, position in a tree, position in a list, and so on. If all you're doing is selecting things by ID, yeah, it's not very helpful. But that's a very limited use case. I'd be curious to hear how you handle those other use cases without a framework. Or do real coders not select on anything other than ID? > Why didn't people, for their own coding convenience's sake stick to it > as they did with innerHTML so that W3C bandits would finally get > forced > to accept it as they did with the previous? The W3C doesn't consistently follow de facto standards. Frameworks generally abstract away browser implementation differences, so even if everyone drops support for innerHTML tomorrow, as long as they replace it with some other way to do the same thing, the same functions will still work in most frameworks. In that same scenario, innerHTML would, of course, stop working. And, of course, there was a time when it wasn't clear innerHTML wouldn't suddenly disappear, so whoever criticized your use of innerHTML years ago so strongly that you still haven't let it go, that person was probably right to do so. > Why are people wasting time on learning how to use javascript > skinners like these, considering other people scripting habits as > their > scripting knowledge, and atop of it forcing clients to download up to > 200KB for a crap like that, instead of learning to code for real. > That is: > javascripting?!! Ha. I remember when real coders used assembly, and then C, and then C+ +, and then Java. It's nice to know real coders can use JavaScript now, if not yet the frameworks used by the non-real coders who are doing all the cool work. Also, I really appreciate the extra touch of this elitism coming from a hotmail address. Comedy gold! Thanks a lot. Peace, Scott From blinkdog at gmail.com Sun Oct 4 21:31:32 2009 From: blinkdog at gmail.com (Noah Sussman) Date: Sun, 4 Oct 2009 22:31:32 -0400 Subject: [Javascript] Hopefully quick question. In-Reply-To: References: Message-ID: On Sat, Oct 3, 2009 at 2:36 PM, Terry Riegel wrote: > > > Which means when a person clicks on obj the code in that function will > fire, which will alert the LAST value of obj. > > I want to pass that function the value of obj at the time it is set up. Use a closure to capture the current value of the variable. var obj = $('mydiv'); obj.onclick=(function () { var localObj = obj; return function (){ alert(localObj.id); } })(); obj = $('myotherdiv'); I have posted code that works for me, here http://gist.github.com/201796 For an explanation of why this works, start with these 2 articles. http://www.crockford.com/javascript/private.html http://www.jibbering.com/faq/faq_notes/closures.html Also there is some good information around p. 131 of the Rhino book. -- Noah Sussman nerdabout.com noah at one more bug dot com Writing is the beating heart of online user experience -- @Zeldman Software is fiction, it is imagination. -- Richard Gabriel From riegel at clearimageonline.com Mon Oct 5 04:28:11 2009 From: riegel at clearimageonline.com (Terry Riegel) Date: Mon, 5 Oct 2009 05:28:11 -0400 Subject: [Javascript] Hopefully quick question. In-Reply-To: References: Message-ID: <876D1FFC-D768-414E-B402-F4DFAE039C1E@clearimageonline.com> Thanks, This closure thing is very cool. I think I understand it until I start using it :) Your answer is spot on. I will read the links you posted and do some testing to see if I can get my head around it. Terry http://p8ste.com On Oct 4, 2009, at 10:31 PM, Noah Sussman wrote: > On Sat, Oct 3, 2009 at 2:36 PM, Terry Riegel > wrote: >> >> >> Which means when a person clicks on obj the code in that function >> will >> fire, which will alert the LAST value of obj. >> >> I want to pass that function the value of obj at the time it is set >> up. > > Use a closure to capture the current value of the variable. > > var obj = $('mydiv'); > obj.onclick=(function () { > var localObj = obj; > return function (){ > alert(localObj.id); > } > })(); > obj = $('myotherdiv'); > > I have posted code that works for me, here > http://gist.github.com/201796 > > For an explanation of why this works, start with these 2 articles. > > http://www.crockford.com/javascript/private.html > http://www.jibbering.com/faq/faq_notes/closures.html > > Also there is some good information around p. 131 of the Rhino book. > > > -- > Noah Sussman > nerdabout.com > noah at one more bug dot com > Writing is the beating heart of online user experience -- @Zeldman > Software is fiction, it is imagination. -- Richard Gabriel > _______________________________________________ > Javascript mailing list > Javascript at lists.evolt.org > http://lists.evolt.org/mailman/listinfo/javascript From talofo at gmail.com Mon Oct 5 18:39:00 2009 From: talofo at gmail.com (MEM) Date: Tue, 6 Oct 2009 00:39:00 +0100 Subject: [Javascript] Newbie - Understanding a piece of code Message-ID: <000801ca4615$048ce300$0da6a900$@com> Hello all, I'm using a validation class because, despite my efforts, I couldn't create a validation class in time, so I'm ashamed for using someone else's code without properly understand it. I really am, so please, spare me for now... I'm already on a bad conscience... :s On the form element, I have:
And more at the bottom, I have: function myOwnSubmit() { //instanciate the validation class //the instance method validate will return boolean value var result = valid.validate(); if (result == true) { document.getElementById('submit_bt').disabled = true; } } I was expecting that, once the onsubmit is called, the validation takes place. Once the validation fields are error clean, (the result is true), it will disable the submit button with the id of 'submit_bt'. However, the button only gets disabled for a few seconds, and worst of all, no form data are getting submitted. I'd like to understand what's going wrong and, above all, what I'm not getting, that I should, in order to properly understand, how something like this should work. Thanks in advance, M?rcio From crenpeva at gmail.com Thu Oct 8 07:13:15 2009 From: crenpeva at gmail.com (=?UTF-8?Q?Cristian_P=C3=A9rez?=) Date: Thu, 8 Oct 2009 08:13:15 -0400 Subject: [Javascript] Imprimir PDF Message-ID: <255de8a90910080513p6ef3d709qc8da6fa0d81e2278@mail.gmail.com> Hola a todos, me presento, soy Cristian y vivo en Chile.Trabajo desde hace mucho tiempo en el area de desarrollo web, especialmente con coldfusion+oracle, y ahora se me present? un problema, que no he podido solucionar. Tengo una pag. web que lista archivos con formato pdf, con un ticket marco cuales quiero imprimir (estos archivos se encuentran en el servidor) y los cargo con un iframe. Y aparecen todos de una vez en el IE. Todo esto, mesclado con algunos html intercalados. Pero al Imprimir, solo me imprime los html y si quie5ro los pdf, tengo que ir al menu tipo acrobat que instancia el navegador. La idea es imprimir cada uno de los documentos marcados o en su defecto, que pueda imprimir automaticamente, sin la caja de dialogo de las impresoras. Espero haber explicado mi situaci?n y en el futuro, espero cooperar con la lista. Gracias. From riegel at clearimageonline.com Fri Oct 23 14:56:13 2009 From: riegel at clearimageonline.com (Terry Riegel) Date: Fri, 23 Oct 2009 15:56:13 -0400 Subject: [Javascript] Question about prototype library, but more to the point about Javascript Message-ID: <8B9AF6BF-FCFF-4DDE-B720-413E01CA72F2@clearimageonline.com> Hello All, I have a function I wrote that uses the prototype library so I hope that doesn't turn anyone away from the core of my question. I will start with what I am attempting to accomplish. I want to periodically execute an XHR request to check in on the status of another XHR request. So I have written a function called checkin... function checkIn(l,p){ checkIn = new PeriodicalExecuter( function(pe){ new Ajax.Request(l,{ method: 'GET', parameters: 'ajaxrequest=TRUE', encoding: 'UTF-8', onComplete: function(t){ logit('checkin()',t.responseText); if (returnErrorCheck(t.responseText)) {this.stop;} clearImage="undefined"; } }); return false; }, p); } I am trying to figure out how to take the PeriodicalExecuter and stop it after it is started. Any ideas? Terry From js0000 at gmail.com Fri Oct 23 15:24:27 2009 From: js0000 at gmail.com (john saylor) Date: Fri, 23 Oct 2009 16:24:27 -0400 Subject: [Javascript] Question about prototype library, but more to the point about Javascript In-Reply-To: <8B9AF6BF-FCFF-4DDE-B720-413E01CA72F2@clearimageonline.com> References: <8B9AF6BF-FCFF-4DDE-B720-413E01CA72F2@clearimageonline.com> Message-ID: hola On Fri, Oct 23, 2009 at 3:56 PM, Terry Riegel wrote: > I am trying to figure out how to take the PeriodicalExecuter and stop > it after it is started. Any ideas? setInterval() clearInterval() [maybe setTimeout()] use the search engine of your choice. -- \js [ - . . . ] From riegel at clearimageonline.com Mon Oct 26 07:39:47 2009 From: riegel at clearimageonline.com (Terry Riegel) Date: Mon, 26 Oct 2009 08:39:47 -0400 Subject: [Javascript] Question about prototype library, but more to the point about Javascript In-Reply-To: References: <8B9AF6BF-FCFF-4DDE-B720-413E01CA72F2@clearimageonline.com> Message-ID: <0005E347-12F2-4224-B27B-B83635D4A816@clearimageonline.com> Hola, Thanks for taking time to respond. I wasn't clear in my question. I will attempt to clarify. I execute this function... function checkIn(l,p){ checkIn = new PeriodicalExecuter( function(pe){ new Ajax.Request(l,{ method: 'GET', parameters: 'ajaxrequest=TRUE', encoding: 'UTF-8', onComplete: function(t){ logit('checkin()',t.responseText); if (returnErrorCheck(t.responseText)) {this.stop;} clearImage="undefined"; } }); return false; }, p); } And it starts this thing that happens every p seconds. Fine enough. Now At some point in the future I want to stop that thing that is firing every p seconds. How do I reference the local variable checkIn to stop it. I would like to do this without making checkIn a global variable as there may be more than one checkIn's running. Any ideas? Terry On Oct 23, 2009, at 4:24 PM, john saylor wrote: > hola > > On Fri, Oct 23, 2009 at 3:56 PM, Terry Riegel > wrote: >> I am trying to figure out how to take the PeriodicalExecuter and stop >> it after it is started. Any ideas? > > setInterval() > clearInterval() > > [maybe setTimeout()] > > use the search engine of your choice. > > -- > \js [ - . . . ] > _______________________________________________ > Javascript mailing list > Javascript at lists.evolt.org > http://lists.evolt.org/mailman/listinfo/javascript From paul at juniperwebcraft.com Mon Oct 26 17:06:10 2009 From: paul at juniperwebcraft.com (Paul Novitski) Date: Mon, 26 Oct 2009 15:06:10 -0700 Subject: [Javascript] Question about prototype library, but more to the point about Javascript In-Reply-To: <4347180.1256593274723.JavaMail.root@n13> References: <8B9AF6BF-FCFF-4DDE-B720-413E01CA72F2@clearimageonline.com> <4347180.1256593274723.JavaMail.root@n13> Message-ID: At 10/26/2009 05:39 AM, Terry Riegel wrote: >function checkIn(l,p){ > checkIn = new PeriodicalExecuter( > function(pe){ > new Ajax.Request(l,{ > method: 'GET', > parameters: 'ajaxrequest=TRUE', > encoding: 'UTF-8', > onComplete: function(t){ > logit('checkin()',t.responseText); > if (returnErrorCheck(t.responseText)) {this.stop;} > clearImage="undefined"; > } > }); > return false; > }, > p); >} > >And it starts this thing that happens every p seconds. Fine enough. >Now At some point in the future I want to stop that thing that is >firing every p seconds. > >How do I reference the local variable checkIn to stop it. I would like >to do this without making checkIn a global variable as there may be >more than one checkIn's running. In the future you'll be able to use getters & setters cross-browser to operate on values within an object, but unfortunately they don't work in IE as of v8. http://www.robertnyman.com/javascript/javascript-getters-setters.html If the necessity of maintaining multiple instances is really your only reason for not using global variables, why not simply keep an array of checkIns and write a set of functions that maintain it? A function that adds a new checkIn locates the first available slot and returns a pointer to that array element. Regards, Paul __________________________ Paul Novitski Juniper Webcraft Ltd. http://juniperwebcraft.com From talofo at gmail.com Tue Oct 27 04:39:41 2009 From: talofo at gmail.com (MEM) Date: Tue, 27 Oct 2009 09:39:41 -0000 Subject: [Javascript] Create a menu in js. Understanding the workflow Message-ID: <000101ca56e9$698ac030$3ca04090$@com> Hello all, I'm on my way for building a menu using javascript. If I search for js menu on google, I get "23523523 free build now menus etc..." But I would like to understand and see, the process involving the creation of a js menu that needs to be unobtrusive. For this database menu I need to: Have a menu that, when the user clicks: a) show the child elements of that clicked parent. b) change the URL. I don't mind about reloading. The server side part: 1) Construct an array from a query. 2) Build a ul/li List based on that array 3) Print it out to the page. The client-side part: 4) I suppose I should I give to the li and ul some ids, so that we can show child elements only when a specific parent is clicked? But we add this ul/li ids on the server side code, or on the client-side code? 5) Each time the user clicks on a menu item, the URL must change. How to preserve the menu estates over this url changes? Some questions may not make too much sense for an experimented look, and are related with my confusion, but what I really like to understand is the procedures for creating something like this. Could I have your help on this? Thanks a lot, M?rcio From david at dorward.me.uk Tue Oct 27 05:18:20 2009 From: david at dorward.me.uk (David Dorward) Date: Tue, 27 Oct 2009 10:18:20 +0000 Subject: [Javascript] Create a menu in js. Understanding the workflow In-Reply-To: <000101ca56e9$698ac030$3ca04090$@com> References: <000101ca56e9$698ac030$3ca04090$@com> Message-ID: <8E0EB207-1CE2-48C6-A819-36E008C2BC70@dorward.me.uk> On 27 Oct 2009, at 09:39, MEM wrote: > I'm on my way for building a menu using javascript. Just what the world needs. > For this database menu I need to: > Have a menu that, when the user clicks: > a) show the child elements of that clicked parent. In order to be accessible to non-pointing device and non-JS users, the menu opening element needs to be a link. For non-JS users, this link will go to a suitable page (usually an index page for the menu section). For keyboard users, it needs to be a link so it can get the focus (in a reliable, cross browser fashion). Have the onclick handler change the className of the parent element (a list item containing the link and submenu). Use a descendent selector with that class name in the stylesheet to change the dislpay style of the submenu. > b) change the URL. http://www.w3.org/TR/html4/struct/links.html#h-12.2 > The server side part: > 1) Construct an array from a query. > 2) Build a ul/li List based on that array > 3) Print it out to the page. All depend on the language you use and the data structure you keep the data in. > The client-side part: > 4) I suppose I should I give to the li and ul some ids, so that we > can show > child elements only when a specific parent is clicked? No need. You just need to identify the top of the menu. Then you can find ul and li elements with in it, and identify their children and parents. > 5) Each time the user clicks on a menu item, the URL must change. > How to > preserve the menu estates over this url changes? Attach a JavaScript event to the "go to page" links. Have it edit those links to include a serialised description of the condition of all the menus (e.g. in the fragment identifier). Read that information in the onload event. -- David Dorward http://dorward.me.uk From talofo at gmail.com Tue Oct 27 05:45:00 2009 From: talofo at gmail.com (MEM) Date: Tue, 27 Oct 2009 10:45:00 -0000 Subject: [Javascript] Create a menu in js. Understanding the workflow In-Reply-To: <8E0EB207-1CE2-48C6-A819-36E008C2BC70@dorward.me.uk> References: <000101ca56e9$698ac030$3ca04090$@com> <8E0EB207-1CE2-48C6-A819-36E008C2BC70@dorward.me.uk> Message-ID: <001401ca56f2$8974ad80$9c5e0880$@com> Thanks David. I've got a killing comment on this, on the php side of things: Quoting: "essentially what you are saying is: 1) user is presented with the basic navigation menu 2) user clicks an item and page navigates somewhere else 3) because of the item user clicked in 2) display some extra menu items That's not a menu, it's just a navigation bar that changes slightly depending on where you are in the site..." So maybe my focus should not be on the click event but on the url changes? Putting the thinks like this, should I still think on js for this job? What do you think? Thanks, M?rcio > -----Original Message----- > From: javascript-bounces at lists.evolt.org [mailto:javascript- > bounces at lists.evolt.org] On Behalf Of David Dorward > Sent: ter?a-feira, 27 de Outubro de 2009 10:18 > To: JavaScript List > Subject: Re: [Javascript] Create a menu in js. Understanding the > workflow > > On 27 Oct 2009, at 09:39, MEM wrote: > > I'm on my way for building a menu using javascript. > > Just what the world needs. > > > For this database menu I need to: > > Have a menu that, when the user clicks: > > a) show the child elements of that clicked parent. > > In order to be accessible to non-pointing device and non-JS users, the > menu opening element needs to be a link. > > For non-JS users, this link will go to a suitable page (usually an > index page for the menu section). > > For keyboard users, it needs to be a link so it can get the focus (in > a reliable, cross browser fashion). > > Have the onclick handler change the className of the parent element (a > list item containing the link and submenu). Use a descendent selector > with that class name in the stylesheet to change the dislpay style of > the submenu. > > > b) change the URL. > > http://www.w3.org/TR/html4/struct/links.html#h-12.2 > > > The server side part: > > 1) Construct an array from a query. > > 2) Build a ul/li List based on that array > > 3) Print it out to the page. > > All depend on the language you use and the data structure you keep the > data in. > > > The client-side part: > > 4) I suppose I should I give to the li and ul some ids, so that we > > can show > > child elements only when a specific parent is clicked? > > No need. You just need to identify the top of the menu. Then you can > find ul and li elements with in it, and identify their children and > parents. > > > 5) Each time the user clicks on a menu item, the URL must change. > > How to > > preserve the menu estates over this url changes? > > Attach a JavaScript event to the "go to page" links. Have it edit > those links to include a serialised description of the condition of > all the menus (e.g. in the fragment identifier). Read that information > in the onload event. > > -- > David Dorward > http://dorward.me.uk > > _______________________________________________ > Javascript mailing list > Javascript at lists.evolt.org > http://lists.evolt.org/mailman/listinfo/javascript From david at dorward.me.uk Tue Oct 27 09:05:47 2009 From: david at dorward.me.uk (David Dorward) Date: Tue, 27 Oct 2009 14:05:47 +0000 Subject: [Javascript] Create a menu in js. Understanding the workflow In-Reply-To: <001401ca56f2$8974ad80$9c5e0880$@com> References: <000101ca56e9$698ac030$3ca04090$@com> <8E0EB207-1CE2-48C6-A819-36E008C2BC70@dorward.me.uk> <001401ca56f2$8974ad80$9c5e0880$@com> Message-ID: On 27 Oct 2009, at 10:45, MEM wrote: > Thanks David. > I've got a killing comment on this, on the php side of things: > > Quoting: > > "essentially what you are saying is: > > 1) user is presented with the basic navigation menu > 2) user clicks an item and page navigates somewhere else If JavaScript is not available, yes. http://icant.co.uk/articles/pragmatic-progressive-enhancement/ Generally speaking, having menus that expand and contract on a single page isn't a good design decision, but it is what I understood you to be asking for, so it is what I described. > That's not a menu, it's just a navigation bar that changes slightly > depending on where you are in the site..." > > So maybe my focus should not be on the click event but on the url > changes? > > Putting the thinks like this, should I still think on js for this job? It depends on the context. The vast majority of sites get along without having menus expanding within the page, and avoiding that 'feature' keeps things simple ? both for the author and the visitor. -- David Dorward http://dorward.me.uk From talofo at gmail.com Tue Oct 27 13:49:29 2009 From: talofo at gmail.com (MEM) Date: Tue, 27 Oct 2009 18:49:29 -0000 Subject: [Javascript] Create a menu in js. Understanding the workflow In-Reply-To: References: <000101ca56e9$698ac030$3ca04090$@com> <8E0EB207-1CE2-48C6-A819-36E008C2BC70@dorward.me.uk> <001401ca56f2$8974ad80$9c5e0880$@com> Message-ID: <005301ca5736$37e18ee0$a7a4aca0$@com> > If JavaScript is not available, yes. > http://icant.co.uk/articles/pragmatic-progressive-enhancement/ Nice article. Very interesting. :) Thanks. > Generally speaking, having menus that expand and contract on a single > page isn't a good design decision, but it is what I understood you to > be asking for, so it is what I described. Of course, my question could be very long, so I decided to cut some parts, but I realize I've cut out the wrong parts. :s > > That's not a menu, it's just a navigation bar that changes slightly > > depending on where you are in the site..." > > > > So maybe my focus should not be on the click event but on the url > > changes? > > > > Putting the thinks like this, should I still think on js for this job? > > > It depends on the context. The vast majority of sites get along > without having menus expanding within the page, and avoiding that > 'feature' keeps things simple ? both for the author and the visitor. I do agree. > David Dorward Thanks for your feedback. Regards, M?rcio From paul at juniperwebcraft.com Tue Oct 27 14:34:43 2009 From: paul at juniperwebcraft.com (Paul Novitski) Date: Tue, 27 Oct 2009 12:34:43 -0700 Subject: [Javascript] Create a menu in js. Understanding the workflow In-Reply-To: <21604244.1256662538125.JavaMail.root@n13> References: <000101ca56e9$698ac030$3ca04090$@com> <8E0EB207-1CE2-48C6-A819-36E008C2BC70@dorward.me.uk> <001401ca56f2$8974ad80$9c5e0880$@com> <21604244.1256662538125.JavaMail.root@n13> Message-ID: At 10/27/2009 07:05 AM, David Dorward wrote: >If JavaScript is not available I would say rather *when* JavaScript is not available... because of course it is never available in some user agents (e.g., search engines, some mobiles, some corporate contexts, etc. etc.). Regards, Paul __________________________ Paul Novitski Juniper Webcraft Ltd. http://juniperwebcraft.com From philthathril at gmail.com Thu Oct 29 14:01:53 2009 From: philthathril at gmail.com (Philip Thompson) Date: Thu, 29 Oct 2009 14:01:53 -0500 Subject: [Javascript] Literal array declarations Message-ID: <73F48269-343D-41B7-9495-AF59CF9E46D5@gmail.com> Hi all. I ran across http://www.jslint.com/ and had it evaluate my code. One of the things it suggested was to "Use the array literal notation []." So, I had: var anArray = new Array(); and JSLint wants me to do: var anArray = []; Well, I'm curious... why is the literal declaration preferred? Thanks, ~Philip From scott at randomchaos.com Thu Oct 29 14:25:35 2009 From: scott at randomchaos.com (Scott Reynen) Date: Thu, 29 Oct 2009 13:25:35 -0600 Subject: [Javascript] Literal array declarations In-Reply-To: <73F48269-343D-41B7-9495-AF59CF9E46D5@gmail.com> References: <73F48269-343D-41B7-9495-AF59CF9E46D5@gmail.com> Message-ID: On Oct 29, 2009, at 1:01 PM, Philip Thompson wrote: > I ran across http://www.jslint.com/ and had it evaluate my code. One > of the things it suggested was to "Use the array literal notation []." > So, I had: > > var anArray = new Array(); > > and JSLint wants me to do: > > var anArray = []; > > Well, I'm curious... why is the literal declaration preferred? It's not preferred by everyone, but it is preferred by Douglas Crockford, the author of JSLint, and here's a quote from one of his books on why he prefers it: http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful/383423#383423 > If you forget to include the new prefix when calling a constructor > function, then this will not be bound to the new object. Sadly, this > will be bound to the global object, so instead of augmenting your > new object, you will be clobbering global variables. That is really > bad. There is no compile warning, and there is no runtime warning Peace, Scott From philthathril at gmail.com Thu Oct 29 15:31:33 2009 From: philthathril at gmail.com (Philip Thompson) Date: Thu, 29 Oct 2009 15:31:33 -0500 Subject: [Javascript] Literal array declarations In-Reply-To: References: <73F48269-343D-41B7-9495-AF59CF9E46D5@gmail.com> Message-ID: On Oct 29, 2009, at 2:25 PM, Scott Reynen wrote: > On Oct 29, 2009, at 1:01 PM, Philip Thompson wrote: > >> I ran across http://www.jslint.com/ and had it evaluate my code. One >> of the things it suggested was to "Use the array literal notation >> []." >> So, I had: >> >> var anArray = new Array(); >> >> and JSLint wants me to do: >> >> var anArray = []; >> >> Well, I'm curious... why is the literal declaration preferred? > > It's not preferred by everyone, but it is preferred by Douglas > Crockford, the author of JSLint, and here's a quote from one of his > books on why he prefers it: > > http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful/383423#383423 > >> If you forget to include the new prefix when calling a constructor >> function, then this will not be bound to the new object. Sadly, this >> will be bound to the global object, so instead of augmenting your >> new object, you will be clobbering global variables. That is really >> bad. There is no compile warning, and there is no runtime warning > > > Peace, > Scott Thanks for your response. So basically it comes down to... he's afraid of being lazy or non-precise. "Hey, it won't work properly if I don't program it correctly." Yeah, big shock there. Okay. Thank you. ~Philip From paul at juniperwebcraft.com Thu Oct 29 16:12:52 2009 From: paul at juniperwebcraft.com (Paul Novitski) Date: Thu, 29 Oct 2009 14:12:52 -0700 Subject: [Javascript] Literal array declarations In-Reply-To: <28699373.1256845816724.JavaMail.root@n13> References: <73F48269-343D-41B7-9495-AF59CF9E46D5@gmail.com> <28699373.1256845816724.JavaMail.root@n13> Message-ID: At 10/29/2009 12:25 PM, Scott Reynen wrote: >It's not preferred by everyone, but it is preferred by Douglas >Crockford, the author of JSLint, and here's a quote from one of his >books on why he prefers it: > >http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful/383423#383423 > > > If you forget to include the new prefix when calling a constructor > > function, then this will not be bound to the new object. Sadly, this > > will be bound to the global object, so instead of augmenting your > > new object, you will be clobbering global variables. That is really > > bad. There is no compile warning, and there is no runtime warning If omitting the "new" in a "new Array()" construction is bad form, why not check for that omission specifically instead of forbidding use of the syntax altogether? I don't mean this as a rhetorical question; I'd really like to understand Douglas' perspective. There are many language features in JavaScript that would cause problems of one sort or another if entered incorrectly. We don't avoid them, we learn to code correctly. Jslint and validators in general can be a wonderful help by proofreading our work in detail. It's when jslint objects to the use of perfectly valid syntax & style not because the code is incorrect or causing a problem but because its misuse might cause a problem in some other script someday that jslint's value as a helper comes into question. I'm seeing jslint not as a validator, a proofreading assistant in everyday scripting, but rather as a set of training wheels whose mission is to teach solid coding practices but that is too strictly idiosyncratic to stay out from underfoot. I would welcome an argument that would soften this perspective. Regards, Paul __________________________ Paul Novitski Juniper Webcraft Ltd. http://juniperwebcraft.com From blinkdog at gmail.com Thu Oct 29 18:36:25 2009 From: blinkdog at gmail.com (Noah Sussman) Date: Thu, 29 Oct 2009 19:36:25 -0400 Subject: [Javascript] Literal array declarations In-Reply-To: References: <73F48269-343D-41B7-9495-AF59CF9E46D5@gmail.com> <28699373.1256845816724.JavaMail.root@n13> Message-ID: On Thu, Oct 29, 2009 at 5:12 PM, Paul Novitski wrote: > If omitting the "new" in a "new Array()" construction is bad form, > why not check for that omission specifically instead of forbidding > use of the syntax altogether? I don't mean this as a rhetorical > question; I'd really like to understand Douglas' perspective. You might be interested in this thread from the JSLint dev list. http://tech.groups.yahoo.com/group/jslint_com/message/592 > There are many language features in JavaScript that would cause > problems of one sort or another if entered incorrectly. We don't > avoid them, we learn to code correctly. Jslint and validators in > general can be a wonderful help by proofreading our work in detail. > It's when jslint objects to the use of perfectly valid syntax & style > not because the code is incorrect or causing a problem but because > its misuse might cause a problem in some other script someday Just my .02 but, for better or worse, I think you have summed up the intent of JSLint rather well in that last sentence. And I think JSLint is largely targeted at developers in "the enterprise," where ad hoc adoption of untested third-party JavaScript, is all too often a common practice. -- Noah Sussman nerdabout.com noah at one more bug dot com Writing is the beating heart of online user experience -- @Zeldman Software is fiction, it is imagination. -- Richard Gabriel