3 ms·
I believe you misunderstood the behaviour of the code. The ninja.yell function is a proper object, it does not disappear and samurai.yell refers to it all the t
by praptak 17y ago
I believe you misunderstood the behaviour of the code. The ninja.yell function is a proper object, it does not disappear and samurai.yell refers to it all the time.
What goes wrong is this: when it tries to call itself recurively by using ninja.yell, ninja already refers to null. That's when it fails.
The moral of this slide is "If you want a function to call itself, better give it a name and refer to it by the name instead of relying on bindings that are outside the scope of the function." This has a followup in the next slide, where exactly this happens.
- axod 17y ago>> "If you want a function to call itself..." Or just use the arguments.callee property which allows you to call the function from itself without worrying about names. edit: oops, just seen that's on a later slide.
- tlrobinson 17y agoExcept in ES5 strict mode :-/
- gb 17y agoYou can still do it, and without a Y combinator or anything like that too: var ninja = { yell: function () { var yellLocal = function(n) { return n > 0 ? yellLocal(n-1) + "a" : "hiy"; } return yellLocal; }() }
- gizmo 17y agoYou're right. I didn't see it was the recursive function that throws the exception.