var self = arguments.callee > a handy inner function reference
DIGG IT!
0
Comments
Published
Friday, November 14, 2003
at
8:50 AM
.
I define a variable self within most of my functions and recently several people asked me why. In short, it makes things easier.\0
In most methods I write, I define a temporary variable "self" as follows:
myFunc = function(){
var self = arguments.callee
}
This is helpful as it puts a friendly name on the executing function instance allowing for all sorts of flexible usage. Say you want a counter for an individual function. Simply call this during function execution:
self.i++
Everytime this is called within a function the variable will be incremented. You can also get to this variable externally without calling the function as follows:
trace(myFunc.i)
Functions are just a special type of Object with attached bytecode. When you call it you can easily store properties within the function itself to persist data beyond the functions execution.
Inner functions based on arguments:
myFunc = function(n){
var self = arguments.callee
self[n](arguments.shift())
}
myFunc.forward= function(){}
myFunc.reverse= function(){}
myFunc.left= function(){}
myFunc.right= function(){}
myFunc('forward',1,2,3)
This is also handy for supporting recursive functions as you can nest and execute functions easily.
Cheers,
ted ;)

0 Responses to “ var self = arguments.callee > a handy inner function reference ”
Post a Comment