2

argumentsオブジェクトのサブセットを取得する方法はありますか? たとえば、最初の引数 (「末尾」) の後の引数のみを選択しますか?

Python では、次のように実行できます。

def tail(*xs):     # * means a tuple of parameters of variable size   
    return xs[1:]  # return from index 1, to the end of the list

tail(1, 2, 3, 4)   # returns (2, 3, 4)

JavaScript で同様のことを行う方法はありますか?

4

1 に答える 1

1

Typically the arguments variable is cast to an array using Array.prototype.slice.call(arguments). As you're already calling the slice method, you can simply add the missing parameters to that function to chop off the end of the pseudo-array:

function tail() {
    return Array.prototype.slice.call(arguments, 1);
}

tail(1, 2, 3, 4); // returns [2, 3, 4]
于 2013-10-16T15:42:20.503 に答える