Prev: A general purpose hash function for javascript
Next: FAQ Topic - My element is named myselect[], how do I access it? (2010-04-09)
From: Thomas Allen on 14 Apr 2010 21:28 On Apr 14, 9:25 pm, Thomas Allen <thomasmal...(a)gmail.com> wrote: > use Array.prototype.filter if it exists, And ML does that: if (Array.prototype.filter) { filter = function(a, fn, context) { return a.filter(fn, context); }; } else { // Note: Array.prototype.reverse is not tested as it is from JS 1.1 if (canCall) { // ... Thomas
From: Thomas 'PointedEars' Lahn on 15 Apr 2010 18:02 Thomas Allen wrote: > Thomas 'PointedEars' Lahn wrote: >> It is easy to see that this would not be equivalent. You would be >> checking the availability of the methods in each iteration. While the >> original code defined a function depending on a one-time check of the >> methods' availability, which is a lot more efficient. > > I noted that, and the more efficient approach follows Please read my whole posting. > I believe the most reasonable, efficient thing to do is to use > Array.prototype.filter if it exists, [...] Certainly yes, but ... > [...] > var can = { > call: typeof Function.prototype.call !== 'undefined', > apply: Function.prototype.call !== 'undefined', > nativeFilter: Array.prototype.filter !== 'undefined' > }; > > var filter; > > if (can.nativeFilter) { .... that test is insufficient for two reasons: 1. You need to test Function.prototype.call(), too, if you call Array.prototype.filter() like below. 2. You need to make sure that Array.prototype.filter refers to a method before you call it. You *really* want to read the archives. > filter = function(arr, fn, ctx) { > return Array.prototype.filter.call(arr, fn, ctx); > }; > } [...] PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Garrett Smith on 15 Apr 2010 20:45 Thomas 'PointedEars' Lahn wrote: > Thomas Allen wrote: > >> Thomas 'PointedEars' Lahn wrote: >>> It is easy to see that this would not be equivalent. You would be >>> checking the availability of the methods in each iteration. While the >>> original code defined a function depending on a one-time check of the >>> methods' availability, which is a lot more efficient. >> I noted that, and the more efficient approach follows > > Please read my whole posting. > >> I believe the most reasonable, efficient thing to do is to use >> Array.prototype.filter if it exists, [...] > > Certainly yes, but ... > >> [...] >> var can = { >> call: typeof Function.prototype.call !== 'undefined', >> apply: Function.prototype.call !== 'undefined', >> nativeFilter: Array.prototype.filter !== 'undefined' >> }; >> >> var filter; >> >> if (can.nativeFilter) { > > ... that test is insufficient for two reasons: > > 1. You need to test Function.prototype.call(), too, if you > call Array.prototype.filter() like below. > Where the more recent `Array.prototype.filter` is implemented, one can assume that `Function.prototype.call` is available with fair safety. > 2. You need to make sure that Array.prototype.filter refers > to a method before you call it. > If `Array.prototype.filter` is present and is not callable object then there is something very wrong with the implementation. Simple existence inference is sufficient. nativeFilter : !!Array.prototype.filter -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Thomas 'PointedEars' Lahn on 15 Apr 2010 21:07 Garrett Smith wrote: > Thomas 'PointedEars' Lahn wrote: >> Thomas Allen wrote: >>> [...] >>> var can = { >>> call: typeof Function.prototype.call !== 'undefined', >>> apply: Function.prototype.call !== 'undefined', >>> nativeFilter: Array.prototype.filter !== 'undefined' >>> }; >>> >>> var filter; >>> >>> if (can.nativeFilter) { >> >> ... that test is insufficient for two reasons: >> >> 1. You need to test Function.prototype.call(), too, if you >> call Array.prototype.filter() like below. >> > > Where the more recent `Array.prototype.filter` is implemented, one can > assume that `Function.prototype.call` is available with fair safety. That is, if nobody has augmented Array.prototype ... >> 2. You need to make sure that Array.prototype.filter refers >> to a method before you call it. > > If `Array.prototype.filter` is present and is not callable object then > there is something very wrong with the implementation. There are no guarantees. > Simple existence inference is sufficient. Never for method calls. > nativeFilter : !!Array.prototype.filter Error-prone. PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: David Mark on 19 Apr 2010 14:46
Thomas 'PointedEars' Lahn wrote: > Thomas Allen wrote: > >> Related, and equally perplexing to me, is the following code: >> >> Updater.prototype.dispatch = function(sEvent, args) { >> Requester.prototype.dispatch.apply(this, sEvent, args); >> >> Is the first argument to that apply call intended, when apply (to the >> best of my knowledge) only accepts two arguments? > > Looks like a refactoring bug and should probably be either Definitely a typo (missing brackets) > > Updater.prototype.dispatch = function(sEvent, args) { > Requester.prototype.dispatch.call(this, sEvent, args); > // ... > }; > > or > > Updater.prototype.dispatch = function(sEvent, args) { > Requester.prototype.dispatch.apply(this, arguments); > // ... > }; > > or > > Updater.prototype.dispatch = function(sEvent, args) { > Requester.prototype.dispatch.apply(this, args); > // ... > }; > Turns out it was none of those. The last two arguments were supposed to be one array. |