• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 6. Building and Calling Functions > Properties in Functions

Properties in Functions

JavaScript functions are objects, and, as such, they contain properties. An important built-in property is length. The length property is a read-only one that returns the number of arguments that are supposed to be in a function. When you define a function, you can put in as many or as few, including zero, arguments in the function as you want. The number of arguments that you include becomes the value of the function’s length.

However, when you actually invoke a function, the number of arguments that you include may not match the number that you defined. The mismatch of defined and invoked arguments need not lead to your program crashing, but if the two are equal, you can be assured that the script is working as structured. A function property, arguments, has a length property as well. From arguments.length, you can find the actual number of arguments used in the function when it is invoked. By comparing the two values, you can debug your script. The following script uses a function with an unused but counted argument in the definition and three unused arguments when invoked. The output tells you whether they match.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint