SoFunction
Updated on 2025-04-06

A brief discussion on javascript

Before writing

In js, we often see the writing method (arguments, 0). Of course, everyone may understand the function of this method, which is to convert the class array object into a real array. Regarding this method, I will tell you my understanding.

This involves the slice() method and the call() method, so let’s briefly talk about these two methods first.

slice() method

Both arrays and strings have this slice method, and the function of this method is to intercept a piece of data. It receives two parameters. The first parameter is the index of the position to be intercepted, and the second parameter is optional, indicating the end position to be intercepted, but does not include the end position. In an array, the return value of the method is an array composed of intercepted elements. In a string, the return value of the method is a string composed of intercepted string.

This method can also pass in negative values. When the parameter is negative, the positive number obtained by adding the parameter to the length of the array or string is used as the actual parameter.

as follows:

[1,2,3,4,5,6].slice(2,4);

[1,2,3,4,5,6].slice(-4,-2);

The return values ​​are all [3,4] and are arrays.

'everything'.slice(2,4);

'everything'.slice(-4,-2);

The return values ​​are 'er' and 'hi' respectively, which are strings.

If a parameter is passed, it is to output all elements from the start position to the end position. No more examples.

Other similar methods for strings

In a string, there are two methods with the slice() method type:

substring() and substr() methods.

Among them, the substring() method returns a string from the start position to the end position, and substr() receives two parameters, the first parameter represents the start position, and the second parameter represents the number of characters to be intercepted, which is slightly different from the first two methods.

When the parameters of the passed method are negative, these three methods are slightly different.

When the parameters of the passed method are negative:

slice(), as mentioned above, is a negative number plus the length of the string to obtain the corresponding positive value;

The parameters of the substring() method are set to zero;

The first parameter of the substr() method is a positive value obtained by adding the string length, and the second parameter is set to zero.

call() and apply() methods

The call() and apply() methods are mainly used to expand the scope of the function.

The call() and apply() methods receive two parameters:

apply(): The first parameter is scope, the second is parameter array, where the second parameter can be an array instance or an argument object.

The call() method also receives two parameters, just because it is different from the parameter transfer method of apply(): the parameters of the transfer function must be written one by one.

Since this is not the focus, I will not repeat it here.

(arguments,0)

In (arguments, 0), the prototype method of Array is called. There is a slice() method for real arrays. However, although some attributes such as length or some class array objects defined by themselves have several attributes, such as length, there is no slice() method. Therefore, for such class array objects, you have to use the prototype method to use the slice() method, that is, (if the slice() method is customized in the custom class array object, then you can naturally call it directly).

Therefore, the meaning of (arguments,0) can be understood as this: For arguments class array, we call the prototype method and use the call() method to limit the scope to arguments. Here it can be understood as arguments. The same parameter 0 is the first parameter of the slice() method, that is, the starting position index. This method converts arguments class array into true array.

Of course, converting arguments into arrays can also be used to traverse, so that the code will naturally be more and not direct enough.

We know that (arguments) can convert objects with length attribute into arrays, except for the node collection under IE (because the dom object under IE is implemented in the form of a com object, and js object and com object cannot be converted)
like:

 var a={length:2,0:'first',1:'second'};
 (a);// ["first", "second"]
 var a={length:2};
 (a);// [undefined, undefined]

Maybe the children's shoes who have just started learning JS are not very good at understanding why this sentence can achieve such a function. For example, I am just one, so let’s explore it.

First of all, there are two uses of slice, one is, the first returns a string, and the second returns an array. Here we look at the second one.

(arguments) Can convert arguments into arrays, then ().slice(); At this point, can we say that the process of (arguments) is to convert the first parameter passed in to an array, and then call slice?
 
Look at the usage of call, as shown in the following example

 var a = function(){
   (this);  // 'littledu'
   (typeof this);   // Object
   (this instanceof String);  // true
 }
 ('littledu');

It can be seen that after calling, the current function is pushed into the scope of the passed parameters. I don’t know if this is right, but anyway, this points to the object passed in, so it’s certain.
At this point, it's basically the same. We can boldly guess the internal implementation of slice, as follows

  = function(start,end){
   var result = new Array();
   start = start || 0;
   end = end || ; //This points to the called object. After using the call, the pointing of this can be changed, that is, to the object passed in. This is the key   for(var i = start; i < end; i++){
      (this[i]);
   }
   return result;
 }

It's probably that's it. Just understand it, don't go into it.

Finally, a general function converted to an array is attached

var toArray = function(s){
  try{
    return (s);
  } catch(e){
      var arr = [];
      for(var i = 0,len = ; i < len; i++){
        //(s[i]);
        arr[i] = s[i]; //It is said that this is faster than push      }
       return arr;
  }
}