1. Function Nesting function
1 functionCalcuate (OPR, A, b) {//define function, OPR----Symbol, A, B--Value2 3 //define + function4 functionAdd (A, b) {5 returnA +b;6 }7 8 //definition-Function9 functionSub (A, b) {Ten returnA +b; One } A - varresult; - the Switch(OPR) {//loop to judge incoming symbols - Case"+": -result = Add (a, b);//Call + function - Break; + Case“-": -result = Sub (A, b);//Call-Function + Break; A } at returnresult; - } - - varRes1 = Calculate ("+", 10, 5);//call in function + function -Console.log (RES1);//Output - varRes2 = Calculate ("-", 10, 5);//Call function inside-function inConsole.log (Res2);//Output 5
2. function return function
In simple terms, the function is used as a type of estoppel for another function.
Define two method functions, a loop function
1 //define the Calculate Rectangle area function2 functionRectanglearea (width, height) {3 varArea = width *height;4 returnArea ;5 }6 //Define compute Triangle Area function7 functionTrianglearea (bottom, height) {8 varArea = 0.5 * Bottom *height;9 returnArea ;Ten } One //Loop function A functionGetarea (type) { - varreturnfunction; - Switch(type) { the Case"rect": -Returnfunction = Rectanglearea;//returns the rectangle calculation function - Break; - Case"Tria": +Returnfunction = Trianglearea;//Returns a triangle calculation function - Break; + } A returnreturnfunction; at } - - varArea = Getarea ("Tria");//Call the triangle calculation function in the loop function -Console.log (Area (10, 15));//output; - varArea = Getarea ("Tria");//Call the rectangle calculation function in the loop function -Console.log (Area (10, 15));//output;
Cocos2d the function method between JS basic syntax calls each other