Foreign JavaScript

I am interested in finding out what this actually does?

if (result = !!(iterator || Prototype.K)(value, index))

I seen this function in the Prototype library and haven’t been able to
find any documentation to gain insight into what the line above does.

any: function(iterator) {
var result = false;
this.each(function(value, index) {
if (result = !!(iterator || Prototype.K)(value, index))
throw $break;
});
return result;
},

More specifically it is the … !!(iterator || Prototype.K)(value,
index) … piece of code that confuses me.

-NSHB


Nathaniel Steven Henry Brown

Open Source Insight - http://nshb.net
Open Source Development - http://inimit.com
Open Source Training - http://osevents.com

It looks like it’s going to call the iterator function that’s passed in
or
Prototype.K if no iterator was passed in. In either case it passes
‘value’
and ‘index’ to the function. I’m guessing that the !! is going to work
to
convert the return value of that function call to a boolean, which is
then
assigned to result.

Prototype.K is just an empty function. So if iterator is not defined
it will just use an empty function instead. The !!() forces the
returned value of the function to be a boolean.

Hope that breif description helps.

Brandon

Just as an additional precision, Prototype.K is not empty, it’s the
identity (Kernel) function: it basically returns its first argument,
untouched. Not to be mistaken with Prototype.EmptyFunction :slight_smile:

Brandon Aaron a écrit :

Prototype.K is just an empty function. So if iterator is not defined
it will just use an empty function instead. The !!() forces the
returned value of the function to be a boolean.

Indeed, the !! trick (double-logical-negation) forces any value to be
converted to its boolean equivalent :slight_smile:


Christophe P. a.k.a. TDD
“[They] did not know it was impossible, so they did it.” --Mark Twain
Email: [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs