Javascript:权威指南(2011)有这个例子(p.186)在严格模式下不起作用,但没有展示如何在严格模式下实现它——我能想到要尝试的事情,但想知道最佳实践/security/performance--在严格模式下做这类事情的最佳方法是什么?这是代码:
// 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.
}