Javascript function properties

Javascript is pretty cool and I’ll admit that I don’t get to do as much as I’d like with it. Today I’m going to briefly cover a few properties of any Javascript function which you can take advantage of:

  • arguments
  • callee
  • arity

arguments

The arguments property contains an array of input parameters which may be passed to the function. It can be used to determine how many variables are being passed in and what they are, without each argument being explicitly declared in the function.

Using the “length” property of the arguments, you can iterate through the array for each parameter passed.

Note that the arguments property can be preceeded by the function name i.e. myFunction.arguments[1].

The arguments array is especially useful with functions that can be called with a variable number of arguments, or with more arguments than they were formally declared to accept.

callee

The callee property can only be called from within a function, when run it will return a string value of the entire function (as if you were viewing the source of the page). Note that you can also use the function.toSource() method to obtain the function source.

arity

The arity property specifies the number of arguments the current function expected to receive. This is different to arguments.length which indicates how many actual arguments were passed in.

Examples

Post a Comment or Leave a Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*