[jQuery] Possible Change to jQuery.map()
OK, so I was trying to do something clever with jQuery.map by using the
Function.call technique to iterate in my root Object's this scope, but
it kept giving me window. Then I read the source and noticed you're
calling the function outright.
[other stuff]
for ( var i = 0; i < elems.length; i++ ) {
var val = fn(elems[i],i);
[yet more stuff]
Since most people don't use 'this' inside .map() and window is always
accessible via 'window' could we change that line to
[other stuff]
for ( var i = 0; i < elems.length; i++ ) {
var val = fn.call(this,elems[i],i);
[yet more stuff]
The main purpose is to make
// within some object's this scope
this.someDomElements = jQuery.map.call(this,new
Array(someVal),function(obj,index) {
return jQuery('<p
class="class'+index+'">').appendTo(this.someDomElement).get(0);
};
possible without resorting to closures like
// within some object's this scope
var self = this;
this.someDomElements = jQuery.map(new Array(someVal),function(obj,index) {
return jQuery('<p
class="class'+index+'">').appendTo(self.someDomElement).get(0);
};
Calling on this is just the first thing that came to mind; if anyone
else has a more intriguing idea using an additional argument or somesuch
it'll be appreciated. I also know I don't really need to write it in
either of those ways, but I figured beauty and function trumps function
alone. It's yet another easy fix that would make things slightly more
extensible for plugin developers and vanilla users alike.
-blair
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/