Đối tượng arguments trong JavaScript là một đối tượng đại diện cho các tham số truyền vào hàm đang thực thi. Cú pháp có hai tham số:
[function.]arguments[p]
Ví dụ
Chạy code sau để hiểu về arguments object trong JavaScript:
<html>
<body>
<script>
function functionArgument(val1, val2, val3) {
var res = "";
res += "Expected Arguments: " + functionArgument.length;
res += "<br />";
res += "Current Arguments : " + arguments.length;
res += "<br />";
res += "Each argument = ";
for (p = 0; p < arguments.length; p++) {
res += "<br />";
res += functionArgument.arguments[p];
res += " ";
}
document.write(res);
}
functionArgument(20, 50, 80, "Demo Text!", "Hello World", new Date());
</script>
</body>
</html>
Giải thích: functionArgument.length cho biết số tham số định nghĩa (3), arguments.length cho biết số tham số thực tế truyền vào (6).