関数judgementで仮引数pointsが設定されている。その関数に仮引数の個数以上の引数を渡すと、その関数内に違う変数があれば、余分な引数は自動でその変数に格納される。もし、関数内に余分な変数がなければエラーになる。
function judgement(points, args){
console.log(points);
console.log(args);
console.log(arguments.length);
console.log(arguments);
console.log(arguments[0]);
console.log(arguments[1]);
let achievement = get_achievement(points);
let pass_or_failure = get_pass_or_failure(arguments[1]);
return (`あなたの成績は${achievement}です。${pass_or_failure}です!`);
}
console.log(judgement(sample1,sample2,sample1));
$ node newjudgement02.js
[ 80, 80, 80, 80 ]
[ 40, 40, 40, 40 ]
3
{ '0': [ 80, 80, 80, 80 ],
'1': [ 40, 40, 40, 40 ],
'2': [ 80, 80, 80, 80 ] }
[ 80, 80, 80, 80 ]
[ 40, 40, 40, 40 ]
あなたの成績はAです。不合格です!
Rubyでは*argsを仮引数に設定しておくと同じことができる。
def hoge(hage, *args)
p hage
p args
end
sample1 = 1
sample2 = 2
sample3 = 3
hoge(sample1,sample2,sample3)
$ ruby test.rb
1
[2, 3]