Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 参数何时成为JS函数中的真正数组_Javascript_Function_Prototype - Fatal编程技术网

Javascript 参数何时成为JS函数中的真正数组

Javascript 参数何时成为JS函数中的真正数组,javascript,function,prototype,Javascript,Function,Prototype,历史上,我们必须做到: var args = Array.prototype.slice.call(arguments) fn.apply(this, args); 获取实际函数调用参数并将其传递给。然而,由于一段时间以来,我可以看到arguments对象确实有可用的slice方法,所以不再需要上面的代码段 问题是-它是什么时候改变的,它是什么,规范更新 编辑:我不清楚上面。。。我的意思是fn.apply(这个,参数)确实有效(我查看了最新的chrome和firefox): 换句话说,参数现

历史上,我们必须做到:

var args = Array.prototype.slice.call(arguments)
fn.apply(this, args);
获取实际函数调用参数并将其传递给。然而,由于一段时间以来,我可以看到arguments对象确实有可用的slice方法,所以不再需要上面的代码段

问题是-它是什么时候改变的,它是什么,规范更新


编辑:我不清楚上面。。。我的意思是
fn.apply(这个,参数)
确实有效(我查看了最新的chrome和firefox):

换句话说,
参数现在可以在apply中直接使用。不久前,它不起作用,必须如上所述传递给Array.prototype.slice.apply。

它不起作用

请尝试以下操作:

var foo = function() {
  console.log(Object.prototype.toString.call(arguments)); 
};

foo(); // [object Arguments]

console.log(Object.prototype.toString.call([])); // [object Array]
请注意,在JS land中,数组和对象之间的差异有点模糊:

var foo = { "0": 1, "1": 2 };
Object.defineProperty(foo, "length", {
  value: 2
});
foo
现在将使用数量惊人的数组方法,使用
call
apply
,等等。

不,它没有

arguments对象是与传递给函数的参数相对应的类似数组的对象

示例:我们希望返回作为数组传递给函数的参数

下面的代码抛出错误,因为我们直接调用
参数
类数组对象上的
切片
方法

function fun(){
    // throws error arguments.slice is not function
    return arguments.slice(0); 
}
当我们将类似于数组的对象转换为数组时,下面的工作正常

function fun(){
    // returns the arguments as array
    return Array.prototype.slice.call(arguments, 0)
}

实际上现在更容易了
Array.from(arguments)
with new ES2016:)@TolgahanAlbayrak很酷,但问题不同;)Firefox中没有适用于我的
arguments.slice()
。但是,随着Rest(splat)操作符的出现,这在ECMA6中失去了它的实用性。
arguments
不是数组。只是这些方法适用于类似数组的对象:具有
.length
属性的对象和具有整数名称的属性。
function fun(){
    // returns the arguments as array
    return Array.prototype.slice.call(arguments, 0)
}