Search code examples
javascriptstrict-mode

There exists a use of arguments.callee with no good alternative?


There is a lot of fallacies about arguments.callee and I'm trying to understand if exists use cases where it really can't be replaced by a viable ES5 strict mode alternative.

In the MDN arguments.callee documentation they point a use of arguments.callee with no good alternative with the following code example below:

function createPerson (sIdentity) {
    var oPerson = new Function("alert(arguments.callee.identity);");
    oPerson.identity = sIdentity;
    return oPerson;
}

var john = createPerson("John Smith");

john();

They inclusive linked a bug to show that in some cases, argument.callee can't be replaced by a code in conformance to ES5 strict mode.

But in understanding, the code they used as example can be replaced with the following strict mode alternative:

"use strict";

function createPerson(sIdentity) {
    var oPerson = function () {
        alert(oPerson.identity);
    };

    oPerson.identity = sIdentity;
    return oPerson;
}

var john = createPerson("John Smith");

john();

With that pointed, there really exists some algorithms where arguments.callee can't be replaced?

BOUNTY

To win the bounty I want the answer to contain a usage of arguments.callee where it will be much more obscure or impossible to use another solution.

In the MDN example, the version I wrote as an alternative doesn't change the usage of that piece of code.


Solution

  • Here is a use case : keep a variable for an inlined event handler (which could come from a generated/templated code) without polluting the global namespace or the DOM or risking other name collisions :

    <button onclick="var f=arguments.callee;alert(f.i=(f.i||0)+1)">CLICK</button>
    

    Demonstration

    Now, let's say it's not a legitimate use. Of course there's none really legitimate or arguments.callee wouldn't have been axed.