JavaScript Replacement for Deprecated arguments.callee

As JavaScript continues to evolve, a long time mainstay of JavaScript, arguments.callee, is going by the wayside. Depending on the time you are reading this, it may have already. arguments.callee was/is a way for JavaScript developers to have a function call itself when there might be some cases when the name of the function is not known or it’s an unnamed anonymous function. As is stated in the Mozilla Developer Network article

callee is a property of the arguments object. It can be used to refer to the currently executing function inside the function body of that function. This is useful when the name of the function is unknown, such as within a function expression with no name (also called “anonymous functions”).

John Resig, know for his involvement with the jQuery project, used it in earlier versions his discussion/example on a pattern of JavaScript inheritance.

/* Simple JavaScript Inheritance
 * By John Resig https://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
 
  // The base Class implementation (does nothing)
  this.Class = function(){};
 
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);        
            this._super = tmp;
           
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
   
    // Populate our constructed prototype object
    Class.prototype = prototype;
   
    // Enforce the constructor to be what we expect
    Class.prototype.constructor = Class;
 
    // And make this class extendable
    Class.extend = arguments.callee;
   
    return Class;
  };
})();

This pattern of adding an “extend” property to an object, is recognizable in other JavaScript projects such as Backbone.js.

Class.extend = arguments.callee;

With arguments.callee being deprecated, What can we do to replace it? The solution can be found in named function expressions. Without going into too much detail about what they are, in the code above. We can change this part…

Class.extend = function(prop) { 
...
}

to the following…

Class.extend = function extender(prop) { 
...
}

Then instead of using arcuments.callee at the bottom we can use…

Class.extend = extender;

By using a named function expression, we have replaced arguments.callee. In doing this we can modernize our applications and sentimentally waive goodbye to relics of the past.

9bit Studios E-Books

Like this post? How about a share?

Stay Updated with the 9bit Studios Newsletter

0 Responses to JavaScript Replacement for Deprecated arguments.callee

    Leave a Reply

    Your email address will not be published. Required fields are marked *