This is the 27th day of my participation in Gwen Challenge

Common operations on class arrays

Traverse the parameter operation

You can get the values of the arguments class array directly inside the function, and you can do something with the arguments

Such as summing parameters

function add(){
	var sum = 0;
	len = arguments.length;
	for(var i = 0; i < len; i++){ sum +=arguments[i];
	}
	return sum;
}
add() / / 0
add(1.2.3.4) / / 10
Copy the code

This means that the parameters can be manipulated inside the function to achieve the desired effect.

Define the link string function

We can use the arguments example to define a function to link strings.

function myConcat(sep){
	var args = Array.prototype.slice.call(arguments.1)// Returns the array generated except for the arguments after the first argument
	return args.join(sep)// The first argument is used as a delimiter to merge
}
myConcat(""."A"."B"."C");
// "A B C"
myConcat("; "."A"."B"."C");
// "A; B; C"
Copy the code

Array. The prototype. Slice. The call (the arguments) to be able to have the arguments are converted to an Array of length attribute, we can understand for is the arguments. The toArray (). The slice ()

This code shows that you can pass any number of arguments to the function and create a concatenated list using each argument as an item in the list. Everyday coding can also be done in this way, abstracting a class of problems to be solved into a general approach to improve code reuse.

Pass parameters using

You can use the Apply method to have one function pass its arguments to another function to use

function A(){
	B.apply(this.arguments) //B needs to borrow the argument from A
}
function B(a,b,c){
	console.log(a,b,c)
}
A(1.2.3) / / 1 2 3
Copy the code

As you can see, A does not have console.log, but actually passes A’s arguments to B, so that B borrows A’s arguments.

Convert a class array to an array
  • Class array borrows array methods into arrays

    Class arrays are not real arrays and do not have the built-in methods of array types, so you need to use the apply and call methods to borrow the methods of arrays. Take the push method of an array.

    var arrayLike = { 
      0: 'java'.1: 'script'.length: 2
    } 
    Array.prototype.push.call(arrayLike, 'A'.'Q'); 
    console.log(typeof arrayLike); // 'object'
    console.log(Object.prototype.toString.call(arrayLike))//[object Object]
    console.log(arrayLike);
    // {0: "java", 1: "script", 2: "A", 3: "Q", length: 4}
    Copy the code

    You can see that the Array method is successful, you can see that it is just a normal object type, there is no push method itself, this place is the Array prototype chain push method, we can implement the class Array push method, to add new elements to the class Array.

    How to convert arguments to an array. We just have to use the method on the array prototype chain that returns an array.

    function sum(a, b) {
      let args = Array.prototype.slice.call(arguments);
     // let args = [].slice.call(arguments); // It works the same way
      console.log(args.reduce((sum, cur) = > sum + cur));
    }
    sum(1.2);  / / 3
    function sum(a, b) {
      let args = Array.prototype.concat.apply([], arguments);
      console.log(args.reduce((sum, cur) = > sum + cur));
    }
    sum(1.2);  / / 3
    Copy the code

    You can see that there are two ways to do this. Array. The prototype. Slice. The call (the arguments), Array. Prototype. Concat. Apply ([], the arguments) can do it, the returned Array can also be successfully reduce using Array method.

  • ES6 method to array

    Array.from method and… Expansion operator

    function sum(a, b) { let args = Array.from(arguments); console.log(args.reduce((sum, cur) => sum + cur)); } sum(1, 2); // 3 function sum(a, b) { let args = [...arguments]; console.log(args.reduce((sum, cur) => sum + cur)); } sum(1, 2); // 3 function sum(... args) { console.log(args.reduce((sum, cur) => sum + cur)); } sum(1, 2); / / 3Copy the code