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 18:42 On Apr 14, 6:36 pm, Thomas Allen <thomasmal...(a)gmail.com> wrote: > On Apr 14, 6:32 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> > wrote: > > > Please learn to quote properly. > > To quote what, exactly? > > Thomas Oh, sorry, I see what you mean, didn't realize until now that I'd left the entire previous message in there. Thomas
From: Thomas Allen on 14 Apr 2010 18:53 On Apr 14, 6:29 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > However, I would not feature-test Function.prototype.call() and then call > Function.prototype.apply(), and vice-versa. The rule of thumb for feature- > testing is: Always test exactly what you are using later. (Perhaps there > is also a canApply in My Library?) Certainly Function.prototype.apply can do anything that Function.prototype.call can, so the thrust of my inquiry is: Does the absence of the former really make the entire following code unnecessary or useless? Or did the author assume that any browser which lacks call would also lack apply? if (canCall) { filter = function(a, fn, context) { var i = a.length, r = [], c = 0; context = context || a; // Didn't want to use in operator and for in loop does not preserve order while (i--) { if (typeof a[i] != 'undefined') { if (fn.call(context, a[i], i, a)) { r[c++] = a[i]; } } } return r.reverse(); }; } There is no variable "canApply", and I only could find one obvious point at which it is checked: if (createXmlHttpRequest && Function.prototype.apply && isHostMethod(global, 'setTimeout')) { API.ajax = (function() { // ... } } Thomas
From: Garrett Smith on 14 Apr 2010 19:41 Thomas Allen wrote: > On Apr 14, 6:29 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> > wrote: [...] > // Didn't want to use in operator and for in loop does not > preserve order > while (i--) { > That looks like reverse loop to populate an array followed by a call to reverse(). He certainly goes to a lot of trouble slow things down. if (typeof a[i] != 'undefined') { > if (fn.call(context, a[i], i, a)) { r[c++] = a[i]; } > } > } > return r.reverse(); [...] -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Thomas 'PointedEars' Lahn on 14 Apr 2010 19:46 Thomas Allen wrote: > Certainly Function.prototype.apply can do anything that > Function.prototype.call can, so the thrust of my inquiry is: Does the > absence of the former really make the entire following code > unnecessary or useless? Or did the author assume that any browser > which lacks call would also lack apply? > > if (canCall) { > filter = function(a, fn, context) { > var i = a.length, r = [], c = 0; > context = context || a; > // Didn't want to use in operator and for in loop does not > preserve order > while (i--) { > if (typeof a[i] != 'undefined') { > if (fn.call(context, a[i], i, a)) { r[c++] = a[i]; } > } > } > return r.reverse(); > }; > } I do not see apply() being called here, so your question does not make sense to me. > There is no variable "canApply", and I only could find one obvious > point at which it is checked: > > if (createXmlHttpRequest && Function.prototype.apply && > isHostMethod(global, 'setTimeout')) { > API.ajax = (function() { > // ... > } > } One would probably want to rewrite this like if (createXmlHttpRequest && isNativeMethod(Function, "prototype", "apply") && isHostMethod(global, 'setTimeout')) { API.ajax = (function() { // ... }; } The `createXmlHttpRequest' test is questionable as it would break (not only fail) if there was no such property on an object in the scope chain (ReferenceError). However, the identifier might be declared an initialized variable or function and nulled on condition afterwards, and then this would be OK. PointedEars -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann
From: Thomas Allen on 14 Apr 2010 20:05
On Apr 14, 7:46 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > Thomas Allen wrote: > > Certainly Function.prototype.apply can do anything that > > Function.prototype.call can, so the thrust of my inquiry is: Does the > > absence of the former really make the entire following code > > unnecessary or useless? Or did the author assume that any browser > > which lacks call would also lack apply? > > > if (canCall) { > > filter = function(a, fn, context) { > > var i = a.length, r = [], c = 0; > > context = context || a; > > // Didn't want to use in operator and for in loop does not > > preserve order > > while (i--) { > > if (typeof a[i] != 'undefined') { > > if (fn.call(context, a[i], i, a)) { r[c++] = a[i]; } > > } > > } > > return r.reverse(); > > }; > > } > > I do not see apply() being called here, so your question does not make > sense to me. apply can do everything that call can, so it doesn't make sense to abandon that block of code if only call is undefined. Thomas |