Problog
This post cover the following topics:
- arguments object
- caller object
- callee object
- introduce a simple stack trace function for all browsers
When i wad first introduced to the arguments object and its caller property i thought about all the cool things i can do with it (yes i’m a geek).
What is arguments Object
The arguments object is accessible within the body of a function. arguments is a special property that refers to an object known as the Arguments object. The Arguments object is an array-like object that allows to access the arguments passed to a function retrieved as an array rather then by name. Lets go over of a few interesting properties of the arguments object:
length - tells you the actual number of parameters passed to the function. This can be used to validate the actual parameters to the number of parameters the programmer defined with the function. For example:
function fn(a,b,c,d){
if(arguments.length != 4 )
throw new Error("Not the right number of arguments");
}
another cool thing that you can do with the arguments object is to write a generic function that takes any number of parameters, for example:
function average(/*any number of parameter*/){
var sum = 0;
for( var i=0;i<arguments.length;i++){
sum += arguments[i];
}
return sum/arguments.length;
}
callee - This is another interesting property of Arguments object. The property refers to the function that is currently being executed. The callee property doesn’t have too many useful implementations (so i think) but you could use it to call recursively to an unnamed function. Whats more interesting about callee is that it has the caller property – arguments.callee.caller
caller - This object points to the Function object that called the current function. This could be used for tracking the stack
Implementing a Simple Stack Tracker
when i first thought about implementing my own stack tracker just for fun (yes, we already established that i’m a geek, let it go!) i implemented it in the most straightforward and simplest way as so:
function getStack(){
var caller = arguments.callee.caller;
var stack = "Stack = ";
while (caller){
stack += "-->"+caller.name;
caller = caller.arguments.callee.caller;
};
return stack;
}
But the problem is that caller.name is only compatible for Firefox, so we need something more generic. If we have used caller.toString() we will get the entire function code – which is not what we need. But, RegExing the function code, we can get the function name:
function getStack(){
fnRE = /function\s*([\w\-$]+)?\s*\(/i;
var caller = arguments.callee.caller;
var stack = "Stack = ";
var fn;
while (caller){
fn = fnRE.test(caller.toString()) ? RegExp.$1 || "{?}" : "{?}";
stack += "-->"+fn;
caller = caller.arguments.callee.caller;
};
return stack;
}
That’s it, it should work on any browser, although i have tested it only on Firefox and IE.
The Next Step
Of course you can develop this function to be more advance to show the parameters and adjust the functionality you can show according to the browser – or you can just use existing libraries like me. after a few searcher i found Eric Wendelin implementation for a stack tracer - very nice implementation.
Enjoy.









hi, nice work.
can you show us how to use it in an example?
THANK YOU
Link | June 29th, 2010 at 6:42 pm
hi, nice work.
can you show us how to use it in an example?
THANK YOU
Link | June 30th, 2010 at 12:42 am
CORY wrote:
Buy:Viagra Soft Tabs.Viagra Professional.Tramadol.Cialis.Viagra Super Force.VPXL.Maxaman.Levitra.Cialis Super Active+.Cialis Professional.Cialis Soft Tabs.Propecia.Soma.Viagra.Zithromax.Super Active ED Pack.Viagra Super Active+….
Link | July 15th, 2010 at 2:32 pm
FRANCIS wrote:
Buy:Zithromax.Cialis Super Active+.Viagra Super Active+.Viagra Super Force.Cialis Professional.Tramadol.Soma.Viagra Professional.Viagra.Super Active ED Pack.Propecia.Cialis.Maxaman.Levitra.Cialis Soft Tabs.VPXL.Viagra Soft Tabs….
Link | July 21st, 2010 at 3:16 pm
GORDON wrote:
Buy:Petcam (Metacam) Oral Suspension.Lumigan.Arimidex.Mega Hoodia.Human Growth Hormone.Zovirax.Accutane.Actos.Prednisolone.Synthroid.Prevacid.Nexium.Valtrex.100% Pure Okinawan Coral Calcium.Retin-A.Zyban….
Link | September 8th, 2010 at 2:28 am
Chrysler wrote:
Chrysler http://ceu.nqs.200i.co : Chrysler…
2005…
Link | September 14th, 2010 at 8:59 pm
GILBERT wrote:
…
BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…
Link | November 11th, 2010 at 3:04 am