Learning Advanced JavaScript from John Resig

Found this posted on reddit today, I thumbed through it and it looks pretty good. He goes through a few topics that are pretty hard to find decent material on, which personally I have been having a bit of trouble with. It looks pretty cool too, kind of a CodeCademy feel, but on more advanced topics.

http://ejohn.org/apps/learn/


If you don’t know who he is:

John Resig is the Dean of Computer Science at Khan Academy and the creator of the jQuery JavaScript library.

http://ejohn.org/about/

2 Likes

A bit dated though. There are a few references to arguments.calle which can only be used with the pre-2011 versions of JavaScript.

As soon as you include "use strict"; to indicate that you are using modern JavaScript the arguments.callee reference will no longer work.

2 Likes

Good point, so what can be done about this problem? The following is not for you specifically Felgall, but will be of some help to others having to deal with this problem.

Instead of using arguments.callee we can name the enclosing function instead, and refer to that named function.

Here’s some problem code, from http://ejohn.org/apps/learn/#15 in this example:

var ninja = { 
  yell: function(n){ 
    return n > 0 ? arguments.callee(n-1) + "a" : "hiy"; 
  } 
}; 
assert( ninja.yell(4) == "hiyaaaa", "arguments.callee is the function itself." );

arguments.callee isn’t supported under the “use strict” directive. The solution is to give the function a name, and use that name instead.

"use strict";
var ninja = { 
  yell: function ninjaYell(n){ 
    return n > 0 ? ninjaYell(n-1) + "a" : "hiy"; 
  } 
};
assert( ninja.yell(4) == "hiyaaaa", "a function can recursively call itself." );
3 Likes

Yeah, I found that later. I thought it was recent when I first found it. The first reference I can find of it was posted 6yrs ago. I actually would have removed this post, but Discourse makes this a little hard. :smiley:

Dated or not, still lots of really good information in there.

And actually, Paul’s alternate solution that avoids arguments.callee is the same as on Resig’s slide #14. It’s only on the next slide where Resig says, “What if we don’t want to give the function a name?” that he introduces arguments.callee. And depending on what browsers you need to support, you may indeed not want to give the function a name. IE8, for example, does bad things with named function expressions.

2 Likes

I agree. There are other browsers that have problems with named functions but as you point out IE8 is the most popular one to have problems.

Of course the problem only occurs where a function needs to call itself (something that is not all that common) and there are alternative ways to define the function without using object notation

A quick test that I just did shows that the following works without needing to either name the function nor use callee

  "use strict";
var ninja = { 
  yell: function(n){ 
    return n > 0 ? this.yell(n-1) + "a" : "hiy"; 
  } 
};

depending on just exactly what it is you are recursive calling for there is usually a way to get the code to work without callee. I am sure that when John updates that tutorial series that the callee references will get replaced.

The only reason I pointed out the callee references in the first place was to let people know that those tutorials had been around for a while and were therefore slightly dated now.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.