substitutes for arguments and arguments.callee in ECMAScript 5
Felipe Gasper
felipe at felipegasper.com
Wed Jan 5 11:30:17 PST 2011
Hi all,
I read with dismay about the deprecation of arguments.callee in
ECMAScript 5.
I can appreciate the security concerns of passing the arguments object
as a parameter to another method, but why not make that the “red flag”
action rather than nixing “arguments” entirely?
e.g. this would cause a problem:
----
function() {
other_func.call(this,arguments);
}
----
..but this would not:
----
function() {
var thisfunc = arguments.callee;
}
----
Myself, I almost always do:
----
function() {
other_func.apply(this,arguments);
}
----
...which would not present the security concerns since the arguments
object itself isn’t passed.
Where I find myself most frequently using the arguments object is to
refer to the function itself, e.g. in YUI:
----
a_dialog.hideEvent.subscribe( function() {
this.hideEvent.unsubscribe( arguments.callee );
console.log("This will only happen once.");
} );
----
Am I to understand now that ECMASCript 5 will now have me type in:
----
var func;
a_dialog.hideEvent.subscribe( func = function() {
this.hideEvent.unsubscribe( func );
console.log("This will only happen once.");
} );
----
...or, worse:
----
var func = function() {
this.hideEvent.unsubscribe( func );
console.log("This will only happen once.");
};
a_dialog.hideEvent.subscribe( func );
----
The example I first gave is simple, straightforward, and (assuming one
knows what arguments.callee is!) easy to grasp. The second is a little
quirky because of the assignment within a function call parameter. The
third is just unclear as to what the code is meant to do.
Granted, (as I have often felt like suggesting) maybe YUI should have a
“subscribeOnce” method for its CustomEvent class for what I want to do.
That aside, though, I think there are many uses for creating anonymous
functions and referring to the function itself within the execution.
Is there really no way to accomplish this moving forward?? Few will miss
“with”, and I welcome being made to set window properties explicitly,
but nixing “callee” seems just unnecessary.
-FG
More information about the es-discuss
mailing list