English 中文(简体)
“Array.prototype.slice.quest' work?
原标题:How does `Array.prototype.slice.call` work?

我知道,它被用于使<代码>arguments 成为真正的Array,但我不理解在使用<代码>Array.prototype.slice. calls(arguments);时发生的情况。

最佳回答

名称下发生的情况是,在<代码>.slice()时,通常称为this, 即为阿雷拉, 而后,它只是随该阿雷拉而变,而且其工作。

如何在<条码>上打上<条码>。 因为:

object.method();

...the object automatically becomes the value of this in the method(). So with:

[1,2,3].slice()

......[1,2,3] Array被定为this.slice()上的价值。


But what if you could substitute something else as the this value? As long as whatever you substitute has a numeric .length property, and a bunch of properties that are numeric indices, it should work. This type of object is often called an array-like object.

www.un.org/chinese/ga/president 因此,如果我们在<编码>上设定<代码>该的价值,则<>slice(>至,.slice(>将仅assume与Array合作,并将做事。

举这个明晰的物体为例。

var my_object = {
     0 :  zero ,
     1 :  one ,
     2 :  two ,
     3 :  three ,
     4 :  four ,
    length: 5
};

这显然不是阿雷,但如果你可以将其定为<条码>,即<条码/条码>值,<条码>,那么它就只能工作,因为它看起来像“阿雷”(<条码>)。

var sliced = Array.prototype.slice.call( my_object, 3 );

http://jsfiddle.net

你们可以看到,结果就是我们期望的:

[ three , four ];

因此,当你制定<条码>条码时,情况就是如此。 标 标 标 标 标 标 标 标 标 标 标 标 :<> 因为arguments has a .length property and a bunch of numeric Index, .slice ( only go about its work as if it is working on a real Array.

问题回答

The arguments object is not actually an instance of an Array, and does not have any of the Array methods. So, arguments.slice(...) will not work because the arguments object does not have the slice method.

由于<代码>arguments的物体与阵列非常相似,因此两种物体是相容的。 这意味着,我们可以使用有争论的阵列方法。 由于阵列方法是用阵列构造的,它们将返回阵列而不是其他论点。

因此,为什么使用<代码>Array.prototype?Array是我们从(new Array(<><>new Array(><>>>>>>>>>)中创建新阵列的物体,这些新阵列如斜体。 这些方法储存在<代码>[Class]。 因此,为了提高效率,我们不要通过<代码>(新轨道)或<代码>(<>/代码>或<代码>查询(>>)而从原型中取而代之。 因此,我们不必开始一个新的阵列。

But why do we have to do this in the first place? Well, as you said, it converts an arguments object into an Array instance. The reason why we use slice, however, is more of a "hack" than anything. The slice method will take a, you guessed it, slice of an array and return that slice as a new array. Passing no arguments to it (besides the arguments object as its context) causes the slice method to take a complete chunk of the passed "array" (in this case, the arguments object) and return it as a new array.

通常,打电话

var b = a.slice();

<代码>a将复制到<条码>。 然而,我们不能这样做。

var a = arguments.slice();

因为arguments n't have slice as a means (it's not a real range).

<代码>Array.prototype.slice =阵列的功能>。slice , with the this Value set to arguments

Array.prototype.slice. calls(arguments)是将论点转化为阵列的老方法。

In ECMAScript 2015, you can use Array.from or the spread operator:

let args = Array.from(arguments);

let args = [...arguments];

首先,请见, 豪华文/a>上的援引工程。 我怀疑,光是回答你的问题就够了。 但这里概述了正在发生的情况:

<代码>Array.prototype.slice 摘录:slicemethods,因为它采用了一种方法(而不是一种功能),因此需要一种环境(一个叫物体,this),否则它就会扔进。 攻击类型 Error: Array.prototype.slice calls on abolition or un Defin.

The call() method allows you to specify a method s context, basically making these two calls equivalent:

someObject.slice(1, 2);
slice.call(someObject, 1, 2);

除前一种要求外,<代码>slice方法将存在于<编码>。 目标原型链(如Array>),而后一链条允许将背景(someObject)手工通过。

Also, the latter is short for:

var slice = Array.prototype.slice;
slice.call(someObject, 1, 2);

这与:

Array.prototype.slice.call(someObject, 1, 2);
// We can apply `slice` from  `Array.prototype`:
Array.prototype.slice.call([]); //-> []

// Since `slice` is available on an array s prototype chain,
 slice  in []; //-> true
[].slice === Array.prototype.slice; //-> true

// … we can just invoke it directly:
[].slice(); //-> []

// `arguments` has no `slice` method
 slice  in arguments; //-> false

// … but we can apply it the same way:
Array.prototype.slice.call(arguments); //-> […]

// In fact, though `slice` belongs to `Array.prototype`,
// it can operate on any array-like object:
Array.prototype.slice.call({0: 1, length: 1}); //-> [1]

Dontabe, 这种行为的低水平基本原理是完全纳入联合材料――工程的类型预测。

许可只是提出反对(根据现有论点:宽恕财产)和在进行所有行动之后所设定的回归阵列。

如果你试图用“INT”价值处理“Sting-method”问题,你可以检验同样的逻辑:

String.prototype.bold.call(11);  // returns "<b>11</b>"

这解释了上述说法。

它使用<代码>slice。 方法阵列及其<条码>,即<条码>。 这就是说,正如你在<条码>上所做的那样。 有这样的方法。

Creating a slice without any arguments will simply take all elements - so it simply copies the elements from arguments to an array.

Array.prototype.slice=function(start,end){
    let res=[];
    start=start||0;
    end=end||this.length
    for(let i=start;i<end;i++){
        res.push(this[i])
    }
    return res;
}

when you do:

Array.prototype.slice.call(arguments) 

arguments becomes the value of this in slice ,and then slice returns an array

Let s assume you have: function.apply(thisArg, argArray )

The apply method invokes a function, passing in the object that will be bound to this and an optional array of arguments.

The slice() method selects a part of an array, and returns the new array.

因此,当您打电话Array.prototype.slice.apply(arguments, [0]。 援引(结合)阵列法的论点。

通常称号为“Array”,然后是“Array”号,然后是“Array”号,并且是其工作。

 //ARGUMENTS
function func(){
  console.log(arguments);//[1, 2, 3, 4]

  //var arrArguments = arguments.slice();//Uncaught TypeError: undefined is not a function
  var arrArguments = [].slice.call(arguments);//cp array with explicity THIS  
  arrArguments.push( new );
  console.log(arrArguments)
}
func(1,2,3,4)//[1, 2, 3, 4, "new"]
/*
    arguments: get all args data include Length .
    slice : clone Array
    call: Convert Object which include Length to Array
    Array.prototype.slice.call(arguments): 
        1. Convert arguments to Array
        2. Clone Array arguments
*/
//normal
function abc1(a,b,c){
    console.log(a);
} 
//argument
function: function abc2(){
    console.log(Array.prototype.slice.call(arguments,0,1))
}

abc1( a , b , c );
//a
abc2( a , b , c );
//a




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.