<!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