Array de-weight, call, apply, bind, the difference between this usage summary

Source: Internet
Author: User

First, the array to weight, directly written to the array prototype chain.

1 //The method can only remove the same number and will not judge that 24 and ' 24 ' are different for all numbers and string numbers are the same and are duplicated2array.prototype.redup=function(){3     varobj={};4      for(vari=0;i< This. length;i++){5         varVal= This[i];6         if(obj[val]== This[i]) {//If you find a duplicate7              This[i]= This[ This. Length-1];//then assign the last value to the current array element.8              This. Pop ();//and remove the last element9i--;//reduce the length of the array so I also want to subtract the next cycle repeat to determine whether the value of the I position is repeatedTen         } Oneobj[val]=Val; A     } -obj=NULL;//free out null -     return  This;//returns an array of this de-weight the } -  -arr=[24,25,24,26,24,25,23,24,25,26]; -Console.log (Arr.redup ());//[24,25,26,23];

Second, the difference between call apply Bind

1 //Call , apply, bind all change the function in the this context2 //call, apply is very similar, the only difference is called is a single argument, except the first parameter is to change this point to other parameters according to the parameters of the function3 //and apply a total of two parameters, the first parameter changes this point, the second parameter is an array, is a collection of function parameters. 4 //call,apply will not only change this point but will execute the function5 //But bind is a little different from the above, the same first argument is to change this point, and the following argument is the argument to the function, but bind does not execute the function.6 functionsum (A, b) {7      This. sum=a+b;8Console.log ( This. sum);9 }Ten functionfn () { One } ASum.call (fn,1,2);//3 This point to FN so FN has the sum property and executes the SUM function -Console.log (fn.sum);//3 -Sum.apply (fn,[2,3]);//5 This point to FN so FN has the sum property and executes the SUM function theConsole.log (fn.sum);//5 -Sum.bind (fn,3,4);//bind does not execute functions so there's nothing here to print -Console.log (fn.sum);//5//bind only changed this point and did not perform the assignment step so fn.sum keep the previous value -Sum.bind (fn,3,4) ();//7//Execute function changes the value of the Fn.sum property and Prints +Console.log (fn.sum);//7 -  + //There's one more thing to be aware of in non-strict mode, regardless of whether the call,bind,apply is not explicitly pointed to, the default is window ASum.call ();//This points to window atSum.call (NULL);//This points to window -Sum.call (undefined);//This points to window - //and in strict mode -Sum.call ();//This point undefined -Sum.call (NULL);//This points to null -Sum.call (undefined);//This point undefined

This usage summary

The first sentence: this is who and where the function is executed and where the definition of all wood has a half-gross money relationship

This is who is divided into several situations:

The first is: the function who calls this is pointing to who, primarily see the function (method) in front of. Who is so this is who has not. That's window.

The second type: In the constructor this refers to the instance in the current constructor (the This of course does not count the method of this is who?). See the first case);

The Third kind: It's call,apply,bind, so just look at it. The first parameter is who then this is who has no parameters? It's a very clear story.

The fourth type: function self-executing like this (function () {}) () Okay, in non-strict mode this is the window and in strict mode this is undefined; So, strict mode to write on the execution of the main body ha.

Array de-weight, call, apply, bind, the difference between this usage summary

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.