Function As Object
Whatever can be created using a constructor has properties and methods above and beyond the obvious, and functions are no exception.
The Function object seems to be
the JavaScript object that’s had the most changes over time. Originally,
the arity property provided the
number of arguments. This has been replaced by calling the length method off of the function name—or by
accessing length on the arguments
array. This, itself, used to be accessible via the function name, but
now is accessible just as “arguments” within the function call. Example 5-7
demonstrates accessing both Function
object properties.
Example 5-7. Examining Function object properties of length and arguments
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Function Object</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
//<![CDATA[
// invoking third argument as function
function funcObject(x,y,z) {
for (var i = 0; i < funcObject.length; i++) {
document.writeln("argument " + i + ": " + arguments[i] + "<br />");
}
}
funcObject(1,2,3);
//]]>
</script>
</body>
</html>In addition, as you’ll see in Chapter 11, when
building custom objects, it’s the function’s ability to reference its
own scope through the keyword this
that’s important for building classes of new objects.
In 1997, I started a set of class objects to manage cross-browser ...
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