$.each "this" context #295

jamietre opened this Issue Oct 22, 2011 · 5 comments


None yet

4 participants


.. it doesn't assign one, $.fn.each on the other hand does. This is really easy to fix of course, and I am interested in contributing to this project, but I wanted to make sure I understand the project goals before I make any pull requests.

  1. For methods that zepto implements, is jQuery compatibility a goal? That is, is there an expectation that code written against zepto will also work with jQuery? I realize it's targeting mobile devices, but I'd love to be able to code in such a way that I can use it for all modern browsers instead of jQuery. Just do a browser check and load jQuery only when it's needed.

  2. Along those lines any reason not to use the existing jQuery tests for methods that are covered?

  3. zepto implements String.prototype.trim, jQuery on the other hand does not but does have things like $.trim and $.inArray (the latter of which I guess would not be needed by any of zepto's targeted browsers). I'm in the habit of using those methods just because they are there and the prototype fills are not with jQuery. Are they left out for philosophical reasons since zepto isn't really targeting old browsers anyway? Or would it be useful to fill in things like this? e.g. where's the line drawn for compatibility in basic stuff vs. size of the library.

If this is discussed somewhere already I apologize and just send me that way. Thanks!

arextar commented Oct 22, 2011

What is the use case for assigning a this value?


It is the current element in the iteration, and its how jquery implements it. A common use of $.each is like

 $.each(someArray,function() {
     // do something with this

If you only need to access the value, it's simpler than using a parameter.

But I guess it just comes down to my basic question, are you going for jquery compatibility? If so then I would think there's no question that such a basic method should function the same way as jquery.

mislav commented Oct 24, 2011

We'd like a patch for $.each behavior, sure!

You can also submit a separate patch for methods like $.trim, $.inArray and possibly others you think are widely used, and we'll think about it. If they don't add a lot to the size, we might take them in. Alternatively, they can be an add-on (a separate file that you have to explicitly include when building your own zepto.js)


Awesome. Thanks. I am still new to Zepto so I'll definitely spend some more time with it before I send anything upstream. I am looking forward to working with it much more.

@jamietre jamietre closed this Oct 24, 2011

I encounted this as well in porting a jQuery plugin issue. I modified the $.each function based on jQuery's (https://github.com/jquery/jquery/blob/master/src/core.js) - it's just using callback.call and passing in the object as the first argument:

 $.each = function(elements, callback) {
  var i, key;
  if (likeArray(elements))
    for(i = 0; i < elements.length; i++) {
      if(callback.call(elements[i], i, elements[i]) === false) return elements;
    for(key in elements) {
      if(callback.call(elements[key], key, elements[key]) === false) return elements;
  return elements;

I tried to go the proper contribution route and run tests, but ruby/rake isn't working on my system at the moment, so I'm not sure if tests pass with this change.

@mislav mislav reopened this Nov 22, 2011
@mislav mislav added a commit that closed this issue Nov 26, 2011
@mislav mislav fix $.each context
Closes #295
@mislav mislav closed this in e796f30 Nov 26, 2011
@lopper lopper added a commit to buddydvd/zepto that referenced this issue Apr 24, 2013
@mislav @lopper mislav + lopper fix $.each context
Closes #295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment