Skip to content Skip to sidebar Skip to footer

Purpose Of Variable

var ninja = { yell: function yell(n){ return n > 0 ? yell(n-1) + 'a' : 'hiy'; } }; assert( ninja.yell(4) == 'hiyaaaa', 'Works as we would expect it to!' );

Solution 1:

The tutorial is showing that removing references to objects does not delete the object itself.

ninja which contained a function yell is shown to have the reference to that function removed, while the reference still exists in a different variable, samurai. Calling the function through samurai even though ninja no longer has it shows that objects persist past the variable referencing them no longer doing so.

Solution 2:

It's to demonstrate that even though the ninja object is no longer referenced, you can still call it and the recursion will work.

Of course, there's two major problems I have with that tutorial.

  1. You can use arguments.callee to call the current function, instead of having to name the function itself. Especially useful for anonymous functions. Deprecated in strict mode, but ask me if I give a rat's arse.

  2. You don't need recursion for this function anyway:

    return "hiy"+newArray(n+1).join("a");
    

Solution 3:

If you notice the previous code: http://ejohn.org/apps/learn/#13

It shows that anonymous functions disappear once the parent object has been cleared.

#14 shows that if you give the anonymous function a name, it would still be able to reference it if set to another variable.

Post a Comment for "Purpose Of Variable"