Hey everyone, this is #23 from John Resig Advanced JavaScript http://ejohn.org/apps/learn/#23, called
What happens if a function is an object pr
Yes, katana
is an object (created using the { ... }
notation). "use" is the name of the property of the object whose value will be the anonymous function (which is also an object).
The function inverts the value of isSharp
(so from true
to false
or false
to true
).
It is asserting that isSharp
is something which does not evaluate to true (this is nearly everything except undefined
, null
, false
, 0
, etc). In this case, since isSharp
is always either true
or false
, it is asserting that it is false
.
The main point (and cool part) of the sample is this line:
katana.use();
This first fetches the value of the "use" property from the katana
object (that's the katana.use
part). The value is the anonymous function from before. Then, that function is executed (that's the ()
part). The really cool part is that it is executed on behalf of the katana
object -- that means this
in the anonymous function is a reference to the katana
object when it's called that way.