关于JavaScript的切片和拼接方法的问题 [英] A question about JavaScript's slice and splice methods

查看:107
本文介绍了关于JavaScript的切片和拼接方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到下面的code来了:

I came across the following code:

var f = function () {
    var args = Array.prototype.slice.call(arguments).splice(1);

    // some more code 
};

基本上,结果 ARGS 是一个数组,它是的复制参没有它的第一个元素。

Basically, the result in args is an array that is a copy of the arguments without its first element.

但我不明白到底是为什么的˚F 参数(这是保存一个对象该函数的输入参数为一个阵列状物体)对象被传递到方法,以及如何片(1)被移除第一元件(定位在索引0)

But what I can't understand exactly is why f's arguments (which is an object that holds the function's inputted arguments into an array-like object) object is being passed to the slice method and how slice(1) is removing the first element (positioned at index 0).

任何人都可以请解释一下吗?

Can anyone please explain it for me?

P.S。在code是这个<一个href=\"http://stackoverflow.com/questions/373157/how-can-i-pass-a-reference-to-a-function-with-parameters\">partial应用功能

推荐答案

&LT;注意>

从实际的code <一个href=\"http://stackoverflow.com/questions/321113/how-can-i-$p$p-set-arguments-in-javascript-function-call-partial-function-applic/321527#321527\">linked答案是:

var args = Array.prototype.slice.call(arguments, 1);

即。 切片,而不是拼接

&LT; /注>

i.e. "slice", not "splice"
</Note>

首先,在方法经常被用来的使数组这就是所谓的副本

First of all, the slice method is often used to make a copy of the array it's called on:

var a = ['a', 'b', 'c'];
var b = a.slice();  // b is now a copy of a
var c = a.slice(1); // c is now ['b', 'c']

所以,简单的答案是,code基本上是模仿:

So the short answer is that the code is basically emulating:

arguments.slice(1); // discard 1st argument, gimme the rest

但是你不能直接这样做。在<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments\">special 参数对象(可用的所有JavaScript函数执行上下文中),但基于阵列的的,它通过支持索引 [] 运营商,数字键,实际上不是一个数组;你不能 .push 到它, .pop 关闭,或 .slice 它,等等。

However you can't do that directly. The special arguments object (available inside the execution context of all JavaScript functions), although Array-like in that it supports indexing via the [] operator with numeric keys, is not actually an Array; You can't .push onto it, .pop off it, or .slice it, etc.

在code实现这一点的方法是欺骗的功能(这又是不提供参数对象)来运行的参数,通过<一个上下文href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call\"><$c$c>Function.prototype.call:

The way the code accomplishes this is by "tricking" the slice function (which again is not available on the arguments object) to run in the context of arguments, via Function.prototype.call:

Array.prototype.slice // get a reference to the slice method
                      // available on all Arrays, then...
  .call(              // call it, ...
    arguments,        // making "this" point to arguments inside slice, and...
    1                 // pass 1 to slice as the first argument
  )

Array.prototype.slice.call(参数).splice(1)完成同样的事情,但使外来调用拼接( 1),其中删除的从数组元素从 Array.prototype.slice.call(参数)开始返回首页 1 ,并继续到数组的结尾。 拼接(1)在IE无法正常工作(它在技术上缺少第二个参数告诉它有多少项目来删除IE浏览器和ECMAScript需要)。

Array.prototype.slice.call(arguments).splice(1) accomplishes the same thing, but makes an extraneous call to splice(1), which removes elements from the array returned from Array.prototype.slice.call(arguments) starting at index 1 and continuing to the end of the array. splice(1) doesn't work in IE (it's technically missing a 2nd parameter telling it how many items to remove that IE and ECMAScript require).

这篇关于关于JavaScript的切片和拼接方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆