function count_times() { count_times.call_count = (count_times.call_count || 0) + 1; print(count_times.call_count); }Trivial example I know, but let your imagination go wild.
Please join me at my new location bryankyle.com
Wednesday, October 17, 2007
Functions ARE Objects!
Javascript is a functional, object-oriented language - but what exactly does that mean to you a developer? It means that functions ARE objects. You can add members to functions just as you could any object. Take for example, the following function that keeps track of how many times it's been called:
Wednesday, October 10, 2007
arguments.callee
Javascript Tip: Ever wonder what arguments.callee is for? Well, it contains the function being evaluated, so you can write anonymous recursive functions like the following:
(function(n) { if (n > 1) return n * arguments.callee(n-1); else return 1; })(5);Which is the same as the following written in a more traditional way:
function fact(n) { if (n > 1) return n * fact(n-1); else return 1; } fact(5);
Subscribe to:
Posts (Atom)