Javascript: The Definitive Guide (2011) hat dieses Beispiel (S.186), das nicht im strikten Modus funktioniert, aber nicht zeigt, wie es im strikten Modus zu implementieren - ich denke an Dinge zu versuchen, aber wundere mich Best Practices/Sicherheit/Leistung - Was ist der beste Weg, solche Dinge im strikten Modus zu tun? Hier ist der Code:Strict-Mode: Alternative zu argument.callee.length?
// 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.
}
warum Community Wiki alles – Raynos
@Raynos: Nur nicht besorgt über SO rep Punkte, denke ich. Lässt die Antwort für andere, die ebenfalls etwas beitragen möchten, einladender erscheinen. –