The function also has two very useful properties: length and prototypefunctionfn1 () {}functionfn2 (num1,num2) {}functionFn3 (NUM1) {}//The length of the function indicates the value of the parameter expected by the functionAlert (fn1.length)//0Alert (fn2.length)//2Alert (fn3.length)//1call and apply methods can be called by the function name of the function has two parameters for apply, the first is the context of the call, the second is an array of parameters, you can directly transfer the arguments into the first parameter is a context object The following parameters are different function parametersfunctionsum (num1,num2) {returnNUM1 +num2;}functionCallSum1 (num1,num2) {//Use the SUM function to complete a call, and the parameter of the call is the parameter of the CALLSUM1 function. //the second parameter of apply represents an array of a set of parameters returnSum,apply ( This, arguments)}functioncallSum2 (num1,num2) {//The key is that the second argument is an array returnSum.apply ( This, [num1,num2]);} Alert (CALLSUM1 (12,22); alert (callSum2 (22,32));functioncallSum3 (num1,num2) {//Call is passed through the parameter list, and the other is not the same as apply. returnSum.call ( This, num1,num2);} Alert (CALLSUM3 (12,22));/*when a class needs to be created, the properties and methods of the set class need to be referenced by the This keyword, but it is particularly important to note that the This keyword will be different when called by different calling objects*/varcolor = ' Red ';functionShowcolor () {alert ( This. color);}//created a class that has a color property and a Show methodfunctionCircle (color) { This. color =color;}varC=NewCircle (' Yellow '); Showcolor.call ( This)//using the context to invoke Showcolor, the result is redShowcolor.call (c);//The context object is, and the result is yellow/*by using call and apply, it is possible to define a method in an object, which is a use of calls and apply.*/
The function deeply understands the properties of the---function length and method call and apply