Authenticity Array Conversion
/*the role of the Apply and call methods: specifically for modifying the This format inside a method: Call (object, parameter 1, parameter 2, ...); Apply (object, [array]);*/functionTest () {Console.log ( This);}//window.test ();varobj = {"Name": "Lnj2"};/*1. Find the test method by Window.test 2. Modify the inside of the test method found by apply (obj) to the custom object*///window.test.apply (obj);//Window.test.call (obj);functionsum (A, b) {Console.log ( This); Console.log (A+b);}//Window.sum.call (obj, 1, 2);/*1. Find the Sum method by Window.sum 2. Modify the inside of the sum method found by apply (obj) to the custom object 3. Takes the elements in the passed-in array, in turn, to the parameters*///window.sum.apply (obj, [3, 5]);//var arr = [];//Arr.push (1);//Console.log (arr);//a procedure for converting a pseudo-array to a true array//var arr = [1, 3, 5, 7, 9];//var obj = {};/*1. Use [].push to find the push method in the array 2. Modify the inside of the push method found by apply (obj) to a custom object 3. The elements in the passed-in array are then fetched, passed to the parameter*///[].push.apply (obj, arr);//console.log (obj);Window.onload =function(EV) {//system comes with pseudo-array, low-level browser no problem varres = Document.queryselectorall ("div"); //custom pseudo-arrays, low-level browsers have problems varobj = {0: "Lnj", 1: "Length:2",}; //var arr = [];//true array //[].push.apply (arr, obj); //Console.log (arr); //If you want to convert a pseudo-array to a true array, you can use the following methods vararr =[].slice.call (obj); Console.log (arr); //var arr2 = [1, 3, 5, 7, 9]; //if the slice method does not pass any parameters, the elements in the array are returned as-is in a new array //var res2 = Arr2.slice (); //var res2 = Arr2.slice (2); //var res2 = Arr2.slice (2, 4); //Console.log (res2);}
Apply and Call methods