JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

8.7.1 The length Property

8.7.1 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 argumentsarray 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.

function check(args) {

var actual = args.length; // The actual number of arguments

var expected = args.callee.length; // The expected number of arguments

if (actual !== expected) // Throw an exception if they differ.

throw Error("Expected " + expected + "args; got " + actual);


function f(x, y, z) {

check(arguments); // Check that the actual # of args matches expected #.

return x + y + z; // Now do the rest of the function normally.

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1