Function Properties, Methods, and Constructor
We’ve seen that functions are values in JavaScript programs. The
typeof operator returns the string
“function” when applied to a function, but functions are really a
specialized kind of JavaScript object. Since functions are objects,
they can have properties and methods, just like any other object.
There is even a Function()
constructor to create new function objects. The subsections that
follow document function properties and methods and the Function() constructor. You can also read
about these in the reference section.
The length Property
Within the body of a function, arguments.length specifies the number of
arguments that were passed to the function. The length property of a function itself,
however, has a different meaning. This read-only property returns
the arity of the function—the number of
parameters it declares in its parameter list, which is usually the
number of arguments that the function expects.
The following code defines a function named check() that is passed the arguments array from another function. It
compares arguments.length (the
number of arguments actually passed) to arguments.callee.length (the number
expected) to determine whether the function was passed the right
number of arguments. If not, it throws an exception. The check() function is followed by a test
function f() that demonstrates
how check() can be used:
// This function uses arguments.callee, so it won't work in strict mode.functioncheck(args){
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access