I am coming back to this book after unable to understand previous chapters(including concept of assert) but now that I am understanding this, I proceed and I was reading chapter 4 when I saw below example. I think part that doesn't make sense to me(even w/ the diagram in the book), why when ninja={} (which I assume then chirp inside of ninja is gone?), why samurai is still able to refer to that anonymous function as basically ninja.chirp?).
I totally get the fix for this.chirp to make it work but I really don't get this concept of why samurai is able to point to chirp which should no longer be available(or is this the closure that i am not fully understanding?
I get the normal closure where function returns and has access to lexical scope but this doesn't look that way.. )
var ninja = {
chirp: function(n){
return n > 1 ? ninja.chirp(n-1) + "-chirp" : "chirp";
}
};
var samurai = { chrip: ninja.chirp };
ninja = {};
try {
assert(samurai.chirp(3) == "chirp-chirp-chirp", "is this going to work?);
}