javascript的func_get_参数

javascript的func_get_参数,javascript,jquery,Javascript,Jquery,如何在数组中获取javascript中的所有函数参数 function(a, b, c){ // here how can I get an array of all the arguments passed to this function // like [value of a, value of b, value of c] } 访问arguments对象 function(a, b, c){ console.log(arguments); console.log(

如何在数组中获取javascript中的所有函数参数

function(a, b, c){

 // here how can I get an array of all the arguments passed to this function
 // like [value of a, value of b, value of c]
}

访问arguments对象

function(a, b, c){
    console.log(arguments);
    console.log(arguments[0]);
    console.log(arguments[1]);
    console.log(arguments[2]);
}

访问arguments对象

function(a, b, c){
    console.log(arguments);
    console.log(arguments[0]);
    console.log(arguments[1]);
    console.log(arguments[2]);
}
使用参数:

for (var i = 0; i < arguments.length; i++) {
  // arguments[i]
}
使用参数:

for (var i = 0; i < arguments.length; i++) {
  // arguments[i]
}
您需要数组对象

更新:参数实际上不是数组,它是一个类似数组的对象。要生成真正的数组,请执行以下操作:

var args = Array.prototype.slice.call(arguments);
您需要数组对象

更新:参数实际上不是数组,它是一个类似数组的对象。要生成真正的数组,请执行以下操作:

var args = Array.prototype.slice.call(arguments);
您可以使用arguments对象,它不是其他答案中所述的数组,它还有一些其他有趣的属性,请参阅 . 当您定义函数本身时,它会在函数的作用域中自动创建

functon bar(arg1,arg2,arg3,...)
{
     console.log(arguments[2]); // gets "arg2"'s value
}
函数对象的属性还有另一种形式:

function foo(a,b,c,d) {
}

var args = foo.arguments;
但是,尽管受到支持,但它已被弃用。

如果使用arguments对象,它不是其他答案中所述的数组,它还有一些其他有趣的属性,请参阅 . 当您定义函数本身时,它会在函数的作用域中自动创建

functon bar(arg1,arg2,arg3,...)
{
     console.log(arguments[2]); // gets "arg2"'s value
}
函数对象的属性还有另一种形式:

function foo(a,b,c,d) {
}

var args = foo.arguments;

但是,尽管它受到支持,但它已被弃用。

谢谢。您知道如何从数组中删除第一个参数吗?我尝试了arguments.slice1,但它说方法不存在arguments对象不是数组,它没有slice方法。修正了我的答案。@Alex:你需要先把它转换成数组。var args=Array.prototype.slice.callarguments;。如果总是要删除第一个元素,请使用shift而不是slicenthanks。您知道如何从数组中删除第一个参数吗?我尝试了arguments.slice1,但它说方法不存在arguments对象不是数组,它没有slice方法。修正了我的答案。@Alex:你需要先把它转换成数组。var args=Array.prototype.slice.callarguments;。如果您总是要删除第一个元素,请使用shift而不是slicenIt。它是一个对象,不是数组,但可以枚举。非常感谢您的编辑,我认为值得向您展示的是,作为一个对象,还有其他有趣的属性可用:向上投票使其成为数组部分。@CRANO:我还认为我应该添加它,因为如果你试着像slice一样调用数组方法,它将不起作用。它是一个对象,而不是数组,虽然是可枚举的。非常感谢你的编辑,我认为值得展示的是,作为一个对象,其他有趣的属性是可用的:向上投票使其成为数组部分。@Cravo:我还认为我应该添加它,因为如果你试着像slice一样调用数组方法,它是不会工作的。