-3
     each: function( object, callback, args ) {
   ...
          if ( callback.apply( object[ name ], args ) === false ) {
            break;
          }
 ...
          if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) {
            break;
          }


    return object;
  }

you can ignore the code maybe .. its jQuery.each implementation from jQuery

i wanted to know what does this line do callback.apply( object[ name ], args ) === false or callback.call( object[ name ], name, object[ name ] ) === false

I mean what all conditions does these construct try to check?

ashish singh
  • 5,232
  • 1
  • 9
  • 27
  • 2
    Have you done any research? `call` and `apply` don't do any checking of conditions. They call functions. They're in the spec, of course ([here](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-function.prototype.call) and [here](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-function.prototype.apply)), and MDN covers them [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) and [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply). – T.J. Crowder Feb 21 '16 at 12:29

1 Answers1

1

callback.apply( object[ name ], args ) is simply calling function callback with object[name] as context (this) and args as arguments. jQuery provides a way to break a loop by returning false, as stated in the docs:

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

So, this piece of code:

if ( callback.apply( object[ name ], args ) === false ) {
  break;
}

checks if the function returns false, and if yes, breaks the loop.


If we skip the context part, the code could look like this (in ES6):

if (callback(...args) === false) {
  break;
}
Michał Perłakowski
  • 70,955
  • 24
  • 137
  • 155
  • ah thanks .. yes i have basic understanding of how call and apply work.. so i reached the point what you seem to be telling.. but is this just a jquery thing?? like if callback is just `return x` and if I do each on an array like [true,false,true,false] .. it runs over complete array and dont break at first `false` – ashish singh Feb 21 '16 at 12:50
  • ok so I looked into `.each` .. so what it means is .. this `call` thing equal to `false` is just a feature given to us by `jQuery.each` to break in between and is no javascript trickery .. will I be correct to say that ? – ashish singh Feb 21 '16 at 12:57