Apply and Call methods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.