Saturday 6 January 2018

javascript - what exactly is "call"/"apply" doing here?

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 href="http://api.jquery.com/jquery.each/" rel="nofollow">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;
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...