JS's Apply and call

Source: Internet
Author: User

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Title</title>    <Script>        //First Use: The function context mode changes the point of this        var Age= +; functionFun () {Console.log ( This. Age); }        varobj={age: -        };        Fun.apply (obj); Fun.call (obj)//Summary: Apply and call, the first parameter function is the same, it is possible to change this point to the first parameter object.         //called for window when the passed object is null        //Application 1: Splicing parameters with "-"        functionfun2 () {//arguments is a masked array that cannot be called by the Join method and is invoked by the Apply transform substitution object.Console.log (Array.prototype.join.apply (arguments, ["-"])); Console.log (Array.prototype.join.call (arguments,"-")); } fun2 (1,2,3,4); //Application 4: Implementing inheritance by borrowing constructors        functionpersion () { This. Name="Zhang San";  This. Age= -; }        functionStudent () {persion.apply ( This); }        varStu=NewStudent ();        Console.log (Stu); //second use: Second to nth parameter processing        functionfun1 (name,work) {Console.log ("name is"+name+"; age is"+ This. Age+"Career"+Work ); }        varobj1={age: -        }; /** Apply and call the second parameter is the parameter of the function * Apply is an array of passes, and when executed, the array is traversed and passed as a parameter. * Call is passed a separate parameter **/fun1.apply (obj1,["Zhang San","Financial"]); Fun1.call (Obj1,"Zhang San","Financial"); //application of 2:apply traversalwindow.onload=function () {            vardivs=Document.queryselectorall ("Div"); varspans=Document.queryselectorall ("span"); vararr=[];            Arr.push.apply (Arr,divs);            Arr.push.apply (Arr,spans);  for(varI=0; I<Arr.length;i++) {Arr[i].style.backgroundcolor="Red"; }        }        //Application 3: Find the maximum value        vararr1=[3,6,1, +, at]; Console.log (Math.max.apply (NULL, arr1)); </Script>    <style>Div,span{Display:Inline-block;width:50px;Height:50px;Border:1px solid Pink;        }    </style></Head><Body><Div></Div><Div></Div><Div></Div><Div></Div><BR><span></span><span></span><span></span><span></span></Body></HTML>

JS's Apply and call

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.