2

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.

sergiogarciadev
  • 2,061
  • 1
  • 21
  • 35
  • Possible duplicate of [Arguments.callee is deprecated - what should be used instead?](http://stackoverflow.com/q/8361642/684229) – Tomas May 25 '14 at 18:31
  • 1
    It is not exactly a duplicate. I'm researching if really are places where we must use `arguments.callee` and not for alternatives. Every piece of code I got I could use an alternative, but I see a lot of people arguing about the deprecation of `arguments.callee` and a I see no reason for that. To use `arguments.callee` we must walk the stack and is slow in every language I know. – sergiogarciadev May 26 '14 at 12:32

3 Answers3

2

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Clever use, inline event handlers introduce an amount of dynamic typing and make browsers go through hoops, which is another reason not to use them. – Benjamin Gruenbaum May 26 '14 at 17:05
  • The usage of `this` make the same result, didn't it? http://jsbin.com/bisitiru/2/edit – sergiogarciadev May 26 '14 at 20:08
  • @SergioGarcia Yes, that's why I precised "without polluting the DOM" : using `this` adds a property to the element and you have a theoretical risk of having another similar event handler having a collision. The solution I give also lets the same function be independent of its use an for example be injected in more than one event handler : http://jsbin.com/nepejo/2/edit – Denys Séguret May 27 '14 at 05:21
0

When you create a function dynamically (from a string), it can't "capture" any outside variables, like a normal closure does.

So, the MDN example simulates the closure by making the value accessible via arguments.callee. There really is no other way without changing the signature of the function.

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
0

I'm afraid you misunderstand the example. It means use the Function constructor there are not alternatives to argument.callee That's say, the code below

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

var john = createPerson("John Smith");

john();

is wrong

xlhuang
  • 221
  • 2
  • 9
  • Yes, I understand you argument, but is there a practical example where it is needed? That is what I'm looking for. Maybe a template parser and output generator or something like it. What I see is people arguing `arguments.callee` is necessary but with no strong reason why. – sergiogarciadev May 26 '14 at 20:20
  • Just for reference, here is another demonstration of a usage of `arguments.callee`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#Example – sergiogarciadev May 26 '14 at 20:23